Re: Need routine to tell me number of dimensions in array.
| From: | Robert Cummings | Date: | Tue, 16 Mar 2010 16:10:54 +0000 |
| Subject: | Re: Need routine to tell me number of dimensions in array. | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-302898@lists.php.net to get a copy of this message | ||
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) {Doh, forgot about that :) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHPforeach ($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