Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/70677-need-help-with-complex-array-counting/
Share on other sites

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.

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);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.