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

[email protected] (Robert Cummings)
Newsgroups php.general
Organization InterJinn
Message-ID <[email protected]>
Peter Lind wrote:
> 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

Doh, forgot about that :)

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP
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.