# Some array counting math

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi there,

I have a function to create an array of all files in a certain folder,
so i can display the structure.
The actual function is below the message, as is an example of its
output.

As one can see, the filesize is also stored in the array (more info
what i need is to sum all bytes per folder.

So if i have e.g.

- folder_1_level1  -> size file 1 / 96
* file_1
* file_2

- folder_1_level2  -> size file 3, 4, 7, 8, 9
* file_3
* file_4
-  folder_1_level3  -> size file 7, 8, 9
* file_7
* file_8
* file_9

- folder_2_level2  -> size file 5, 6
* file_5
* file_6

The latter should give me the sum of all filesizes in there,
folder_level2 should give me folder_level3's size PLUS other files/
folders that are in folder_level2, and then, folder_level1 should have
the size of all files in there down the entire tree.

I hope it's clear and that someone can help me.

Cheers.

*** FUNCTION CODE ***

function mapit( \$dir = '.', \$loop = 0, \$parent = false ){
\$handle = @opendir( \$dir );
while( ( \$file=readdir( \$handle ) ) !== false ){
if( \$file != '.' && \$file != '..' ){
\$point = \$dir."/".\$file;
\$fileSize = filesize( \$dir.'/'.\$file );
if( is_dir( \$point ) )
\$info['dirs'][\$file] = mapit( \$point , \$loop+1, \$file );
else
\$info['files'][\$file] = \$parent.' -> '.\$fileSize;
}
}
return \$info;
}

\$structure = mapit();
print_r( \$structure );

*** /FUNCTION CODE ***

*** RESULT EXAMPLE ***

Array
(
[dirs] => Array
(
(
[files] => Array
(
)

[dirs] => Array
(
[img] => Array
(
[files] => Array
(
[0.jpg] => img -> 36866
[12.jpg] => img -> 218
[13.jpg] => img -> 36866
)
)
)
)

[files] => Array
(
[dirs] => Array
(
[just-a-dir] =>
[images] => Array
(
[files] => Array
(
[lion.jpg] => flip ->
350398
)
)
)
[files] => Array
(
[logo.pdf] => files -> 157764
[tiger.jpg] => files -> 350398
)
)
)

[files] => Array
(
[ftp.php] =>  -> 10238
[cats.php] =>  -> 8237
)
)

*** /RESULT EXAMPLE ***

## Re: Some array counting math

This should work. This function recursively goes through a given array
Note that I had to use regex to get the numerical size as you have
this string, '->' , in there.

function getSize(\$map) {
\$size = 0;
foreach(\$map as \$m) {
if(!is_array(\$m)) {
preg_match("/[0-9]/", \$m, \$p); // regex to get the size
\$size += \$p[0];
} else {
\$size += getsize(\$m);
}
}
return \$size;
}

\$structure = mapit();

--

## Re: Some array counting math

Thank you very much for your quick help!
Not to be a nag though, but if i store other numerical values in the
array (like filemtime), wouldn't they be counted as well?

Would it be better to give each file a ['size'] key? In the best case,
i thought, it would be best to somehow perform the count at the same
time as doing the first loop, so you wouldn't have to run unnecessary
loop, or am i wrong?

Thanks a lot again though!!

(The '->' part doesn't have to be there, it was merely a visual
"guide" for me ...)

## Re: Some array counting math

np ;)

Yes, extra keys would cause the function not to work.
Like you said, a 'size' key would get rid of all the confusion. If you
get it there, simply get rid of the regex line and add the value of
the 'size' key to \$size.

--

## Re: Some array counting math

Great, thanks a lot! I'll probably sadly enough won't have any time
before tomorrow to take a look at it, but i'm confident i can get it
to work! :)

And my remark of doing the counting loop at the same time as the
initial loop, or is that peanuts (in calculating power/speed) for PHP?

Thanks again for helping me so far!