Re: [PHP] Need routine to tell me number of dimensions in array.

[email protected] (Peter Lind)
Newsgroups php.general
Message-ID <[email protected]>
This is one example where references actually decrease memory usage.
The main reason is the recursive nature of the function. Try

<?php

echo memory_get_usage() . PHP_EOL;
$array = range(0,1000000);
$array[10] = range(0,10);
$array[20] = range(0,10);
$array[30] = range(0,10);
$array[40] = range(0,10);
$array[50] = range(0,10);
$array[60] = range(0,10);
$array[70] = range(0,10);
$array[80] = range(0,10);
$array[90] = range(0,10);
$array[100] = range(0,10);
echo memory_get_usage() . PHP_EOL;
carray($array);
function carray ($array)
{
    foreach ($array as $value)
    {
        if (is_array($value)) carray($value);
    }
    echo memory_get_usage() . PHP_EOL;
    echo count($array) . PHP_EOL;
}
echo memory_get_usage() . PHP_EOL;

And then compare with:

<?php

echo memory_get_usage() . PHP_EOL;
$array = range(0,1000000);
$array[10] = range(0,10);
$array[20] = range(0,10);
$array[30] = range(0,10);
$array[40] = range(0,10);
$array[50] = range(0,10);
$array[60] = range(0,10);
$array[70] = range(0,10);
$array[80] = range(0,10);
$array[90] = range(0,10);
$array[100] = range(0,10);
echo memory_get_usage() . PHP_EOL;
carray($array);
function carray (&$array)
{
    $i = 0;
    foreach ($array as $value)
    {
        if (is_array($value)) carray($value);
    }
    echo memory_get_usage() . PHP_EOL;
    echo count($array) . PHP_EOL;
}
echo memory_get_usage() . PHP_EOL;

The memory usage spikes in the first example when you hit the second
array level - you don't see the same spike in the second example.

Regards
Peter

On 16 March 2010 15:46, Robert Cummings <[email protected]> wrote:
>
>
> Richard Quadling wrote:
>>
>> On 15 March 2010 23:45, Daevid Vincent <[email protected]> wrote:
>>>
>>> Anyone have a function that will return an integer of the number of
>>> dimensions an array has?
>>
>> /**
>>  * Get the maximum depth of an array
>>  *
>>  * @param array &$Data A reference to the data array
>>  * @return int The maximum number of levels in the array.
>>  */
>> function arrayGetDepth(array &$Data) {
>>        static $CurrentDepth = 1;
>>        static $MaxDepth = 1;
>>
>>        array_walk($Data, function($Value, $Key) use(&$CurrentDepth,
>> &$MaxDepth) {
>>                if (is_array($Value)) {
>>                        $MaxDepth = max($MaxDepth, ++$CurrentDepth);
>>                        arrayGetDepth($Value);
>>                        --$CurrentDepth;
>>                }
>>        });
>>
>>        return $MaxDepth;
>> }
>>
>> Extending Jim and Roberts comments to this. No globals. By using a
>> reference to the array, large arrays are not copied (memory footprint
>> is smaller).
>
> Using a reference actually increases overhead. References in PHP were mostly
> useful in PHP4 when assigning objects would cause the object to be copied.
> But even then, for arrays, a Copy on Write (COW) strategy was used (and is
> still used) such that you don't copy any values. Try it for yourself:
>
> <?php
>
> $copies = array();
> $string = str_repeat( '*', 1000000 );
>
> echo memory_get_usage()."\n";
> for( $i = 0; $i < 1000; $i++ )
> {
>    $copies[] = $string;
> }
> echo memory_get_usage()."\n";
>
> ?>
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



-- 
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
Flickr: http://www.flickr.com/photos/fake51
BeWelcome: Fake51
Couchsurfing: Fake51
</hype>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.