Some array counting math - PHP

This is a discussion on Some array counting math - PHP ; 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 ...

+ Reply to Thread
Results 1 to 5 of 5

Some array counting math

  1. Default Some array counting math

    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
    will be added, like filemtime),
    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
    (
    [upload] => Array
    (
    [files] => Array
    (
    [index.htm] => upload -> 1391
    [index.php] => upload -> 23
    [upload.php] => upload -> 7733
    )

    [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 ***

  2. Default Re: Some array counting math

    On Nov 27, 7:25 pm, frizzle <phpfriz...@gmail.com> wrote:
    > 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
    > will be added, like filemtime),
    > 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
    > (
    > [upload] => Array
    > (
    > [files] => Array
    > (
    > [index.htm] => upload -> 1391
    > [index.php] => upload -> 23
    > [upload.php] => upload -> 7733
    > )
    >
    > [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 ***


    This should work. This function recursively goes through a given array
    and adds up the sizes.
    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]{1,20}/", $m, $p); // regex to get the size
    $size += $p[0];
    } else {
    $size += getsize($m);
    }
    }
    return $size;
    }

    $structure = mapit();
    echo getsize($structure['dirs']['upload']);

    --
    Kailash Nadh | http://kailashnadh.name

  3. Default Re: Some array counting math

    On Nov 27, 10:16 pm, Kailash Nadh <kailash.n...@gmail.com> wrote:
    > On Nov 27, 7:25 pm, frizzle <phpfriz...@gmail.com> wrote:
    >
    >
    >
    > > 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
    > > will be added, like filemtime),
    > > 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
    > > (
    > > [upload] => Array
    > > (
    > > [files] => Array
    > > (
    > > [index.htm] => upload -> 1391
    > > [index.php] => upload -> 23
    > > [upload.php] => upload -> 7733
    > > )

    >
    > > [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 ***

    >
    > This should work. This function recursively goes through a given array
    > and adds up the sizes.
    > 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]{1,20}/", $m, $p); // regex to get the size
    > $size += $p[0];
    > } else {
    > $size += getsize($m);
    > }
    > }
    > return $size;
    >
    > }
    >
    > $structure = mapit();
    > echo getsize($structure['dirs']['upload']);
    >
    > --
    > Kailash Nadh |http://kailashnadh.name


    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 ...)

  4. Default Re: Some array counting math

    On Nov 27, 9:45 pm, frizzle <phpfriz...@gmail.com> wrote:
    > On Nov 27, 10:16 pm, Kailash Nadh <kailash.n...@gmail.com> wrote:
    >
    >
    >
    > > On Nov 27, 7:25 pm, frizzle <phpfriz...@gmail.com> wrote:

    >
    > > > 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
    > > > will be added, like filemtime),
    > > > 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
    > > > (
    > > > [upload] => Array
    > > > (
    > > > [files] => Array
    > > > (
    > > > [index.htm] => upload -> 1391
    > > > [index.php] => upload -> 23
    > > > [upload.php] => upload -> 7733
    > > > )

    >
    > > > [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 ***

    >
    > > This should work. This function recursively goes through a given array
    > > and adds up the sizes.
    > > 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]{1,20}/", $m, $p); // regex to get the size
    > > $size += $p[0];
    > > } else {
    > > $size += getsize($m);
    > > }
    > > }
    > > return $size;

    >
    > > }

    >
    > > $structure = mapit();
    > > echo getsize($structure['dirs']['upload']);

    >
    > > --
    > > Kailash Nadh |http://kailashnadh.name

    >
    > Thank you very much for your quick help!


    np

    > 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 ...)


    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.

    --
    Kailash Nadh | http://kailashnadh.name

  5. Default Re: Some array counting math

    On Nov 27, 10:51 pm, Kailash Nadh <kailash.n...@gmail.com> wrote:
    > On Nov 27, 9:45 pm, frizzle <phpfriz...@gmail.com> wrote:
    >
    >
    >
    > > On Nov 27, 10:16 pm, Kailash Nadh <kailash.n...@gmail.com> wrote:

    >
    > > > On Nov 27, 7:25 pm, frizzle <phpfriz...@gmail.com> wrote:

    >
    > > > > 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
    > > > > will be added, like filemtime),
    > > > > 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
    > > > > (
    > > > > [upload] => Array
    > > > > (
    > > > > [files] => Array
    > > > > (
    > > > > [index.htm] => upload -> 1391
    > > > > [index.php] => upload -> 23
    > > > > [upload.php] => upload -> 7733
    > > > > )

    >
    > > > > [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 ***

    >
    > > > This should work. This function recursively goes through a given array
    > > > and adds up the sizes.
    > > > 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]{1,20}/", $m, $p); // regex to get the size
    > > > $size += $p[0];
    > > > } else {
    > > > $size += getsize($m);
    > > > }
    > > > }
    > > > return $size;

    >
    > > > }

    >
    > > > $structure = mapit();
    > > > echo getsize($structure['dirs']['upload']);

    >
    > > > --
    > > > Kailash Nadh |http://kailashnadh.name

    >
    > > Thank you very much for your quick help!

    >
    > np
    >
    > > 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 ...)

    >
    > 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.
    >
    > --
    > Kailash Nadh |http://kailashnadh.name


    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!

+ Reply to Thread

Similar Threads

  1. counting checkboxes?
    By Application Development in forum Javascript
    Replies: 6
    Last Post: 07-20-2007, 12:57 AM
  2. Replies: 7
    Last Post: 07-10-2007, 09:40 AM
  3. Counting/comparing
    By Application Development in forum verilog
    Replies: 1
    Last Post: 03-10-2007, 02:21 PM
  4. How to counting solutions ?
    By Application Development in forum PROLOG
    Replies: 4
    Last Post: 07-13-2005, 02:38 AM
  5. Counting Records
    By Application Development in forum basic.visual
    Replies: 1
    Last Post: 04-03-2004, 07:58 AM