Nolongerused3921 Posted September 25, 2007 Share Posted September 25, 2007 I'm having an incredibly hard time trying to solve this problem... I have an array similar to the following: [area1] => Array ( [subarea1] => Array ( [subsubarea1] => Array ( [0] => file1.txt [1] => file2.pdf ) [0] => blah1.txt [1] => blah.2.txt [2] => etc.mp3 ) [subarea2] => Array ( [subsubarea2.1] => Array ( [0] => blah.txt ) [subsubarea2.1] => Array ( [0] => file1.txt [1] => file2.pdf [2] => file3.mpg ) ) ) And I need to go through and count all the files (The [ #]s) in each of the sub arrays... But heres the problem - I need to track how many files are in each folder, and all folders underneath it. Basically, what it boils down to - is I need to find out how many files are in any given folder, including all of it's sub folders - so I can let the user know how many files are in a folder (Obviously). If anyone can think of a way to cycle through the array and get my answer - that'd be great... If you can think of another, better way to find out how many files are in a folder, and all of it's child folders, then that'd be better Thanks in advanced to anyone who saves me the extra few hours of pulling my hair out... I really hate working with filesystems. Quote Link to comment https://forums.phpfreaks.com/topic/70677-need-help-with-complex-array-counting/ Share on other sites More sharing options...
GingerRobot Posted September 25, 2007 Share Posted September 25, 2007 How about this: <?php function no_of_files($start_dir,$sub_dir=FALSE){ $no_of_files=0; if($sub_dir === false){ $handler = opendir($start_dir); }else{ $start_dir = $start_dir.$sub_dir; $handler = opendir($start_dir); } while(false !== ($file = readdir($handler))){ if($file != '.' && $file != '..'){ if(is_dir($start_dir.'\\'.$file)){//if this is a directory, recall the function with the subdirectory defined $dir_name = '\\'.$file; echo $dir_name.'<br />'; $no_of_files += no_of_files($start_dir,$dir_name); }else{//is a file, so increase our counter $no_of_files++; } } } return $no_of_files; } echo no_of_files('C:\Documents and Settings\Ben\My Documents\My Music'); ?> I made it for a post a while back. Counts the number of files in any directory and all sub directories. Quote Link to comment https://forums.phpfreaks.com/topic/70677-need-help-with-complex-array-counting/#findComment-355257 Share on other sites More sharing options...
GingerRobot Posted September 25, 2007 Share Posted September 25, 2007 In response to your original question of counting the number of elements within your multidimension array, this should work: <?php $array = array(1,2,array(1,2,3,array(1,2,3)),3,4,array(array(array(1,2)))); echo '<pre>'; print_r($array); echo '</pre>'; function count_array($array){ $num = 0; foreach($array as $v){ if(is_array($v)){ $num += count_array($v); }else{ $num++; } } return $num; } echo 'number of items in your array: '.count_array($array); ?> Quote Link to comment https://forums.phpfreaks.com/topic/70677-need-help-with-complex-array-counting/#findComment-355261 Share on other sites More sharing options...
Nolongerused3921 Posted September 25, 2007 Author Share Posted September 25, 2007 That works pretty well, but I need to get how many files are in each folder specifically, not a grand total. Like, theres 9 files in area1 5 files in subarea1, 2 in subsubarea1, etc. Quote Link to comment https://forums.phpfreaks.com/topic/70677-need-help-with-complex-array-counting/#findComment-355262 Share on other sites More sharing options...
Nolongerused3921 Posted September 26, 2007 Author Share Posted September 26, 2007 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/70677-need-help-with-complex-array-counting/#findComment-355519 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.