PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 18th, 2004, 11:27 PM
Nemozob Nemozob is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 15 Nemozob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 14 sec
Reputation Power: 0
Creating arrays dynamically with a While loop

After the first 'for' loop it dumps the contents of the first directory into the $files array, and on the second 'for' run it adds the contents of second directory into the $files array. How can I create a unique array name on the second 'for' loop so that I have arrays that contain the contents of each pass? I thought could make it $files1 and $files2 but doing this: $files.$i[] doesn't work.

PHP Code:
<?php

// assume I already got this array with a readdir, while loop.
$myDirs = array('docs','images');

for (
$i=0$i<=1$i++) {

    
$newDir opendir($myDirs[$i]);
    
$newFiles =  readdir($newDir);

    while (
$filename readdir($newDir)){
        if (
$filename != ".." && $filename != "." && $filename != ".DS_Store") {
            
$files[] = $filename;
        }
    }
}

?>

Reply With Quote
  #2  
Old October 19th, 2004, 01:12 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
have you thought about multiple dimension arrays?

so you could have a structure like $files['dirname']['filename']

then again, I'm not quite sure what you're trying to accomplish... perhaps some clarification?

Reply With Quote
  #3  
Old October 19th, 2004, 09:14 AM
m3rajk m3rajk is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 24 m3rajk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
you could try nested loops.
Quote:
Originally Posted by Nemozob
After the first 'for' loop it dumps the contents of the first directory into the $files array, and on the second 'for' run it adds the contents of second directory into the $files array. How can I create a unique array name on the second 'for' loop so that I have arrays that contain the contents of each pass? I thought could make it $files1 and $files2 but doing this: $files.$i[] doesn't work.

PHP Code:
<?php
  
  
// assume I already got this array with a readdir, while loop.
  
$myDirs = array('docs','images');
  for(
j=0;j<numDirs;<j++){
    
$fileset=$currentDir.'files'$fileset=new array[];
for (
$i=0$i<=1$i++) {
  
      
$newDir opendir($myDirs[$i]);
      
$newFiles =  readdir($newDir);
  
      while (
$filename readdir($newDir)){
        if (
$filename != ".." && $filename != "." && $filename != ".DS_Store") {
              
$files[] = $filename;
          }
      }
  }
  }
  
?>

Reply With Quote
  #4  
Old October 21st, 2004, 02:04 PM
Nemozob Nemozob is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 15 Nemozob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 14 sec
Reputation Power: 0
creating multiple dimensional array

Quote:
Originally Posted by MadCowDzz
have you thought about multiple dimension arrays?

so you could have a structure like $files['dirname']['filename']

then again, I'm not quite sure what you're trying to accomplish... perhaps some clarification?


Yeah, say I have two directories, 'docs' and 'images'. Right now I'm getting this info but scanning my directory:

PHP Code:
 $dir opendir('.');
$dirRead =  readdir($dir);

    while (
false !== ($dirRead readdir($dir))) {
        if (
$dirRead != ".." && $dirRead != "." && $dirRead != ".DS_Store" && $dirRead != "index.php") {
            
$myDirs[] = $dirRead;
        }
    } 


but this produces
$myDirs = array('docs','images');
and I want to get to this...
$myDirs = array('docs'=> array(), 'images' => array());

How do I get there? Later I'll add to those secondary arrays with the filenames. Make sense?

Reply With Quote
  #5  
Old October 21st, 2004, 10:58 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
did you check out some of the comments in the PHP Manual for the readdir listing?

I think Rick has a solution you can use...

try the following code the way it is:
PHP Code:
function ls($dir){
    
$handle opendir($dir);
    for(;(
false !== ($readdir readdir($handle)));){
        if(
$readdir != '.' && $readdir != '..'){
            
$path $dir.'/'.$readdir;
            if(
is_dir($path))    
                
$output[$readdir] = ls($path);
            if(
is_file($path))
                
$output[] = $readdir;
        }
    }
    return isset(
$output)?$output:false;
    
closedir($handle);
}

$files ls('.');
print_r($files['docs']); 

Reply With Quote
  #6  
Old October 21st, 2004, 10:59 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Forgot to mention, refer to Rick's post here: http://ca.php.net/readdir

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Creating arrays dynamically with a While loop


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT