Re: Need routine to tell me number of dimensions in array.
| From: | Robert Cummings | Date: | Tue, 16 Mar 2010 16:43:45 +0000 |
| Subject: | Re: Need routine to tell me number of dimensions in array. | ||
| References: | 1 2 3 4 5 6 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-302901@lists.php.net to get a copy of this message | ||
Peter Lind wrote:
Hmm, will probably have to look inside PHP for this ... the foreach loop will copy each element as it loops over it (without actually copying, obviously), however there's no change happening to the element at any point and so there's nothing to suggest to the copy-on-write to create a new instance of the sub-array. It should look like this: $a = array(0, 1, 2, array(0, 1, 2, 3), 4, 5, 6, .... n); $b = $a[3]; doStuffs($b); Whether or not you loop over $a and thus move the internal pointer, you don't change (well, shouldn't, anyway) $b as that's a subarray which has it's own internal pointer, that isn't touched. Or maybe I've gotten this completely backwards ...I'm not sure of the exact reason... PHP has the following comment:
Note: Unless the array is referenced, foreach operates on a copy
of the specified array and not the array itself. foreach
has some side effects on the array pointer. Don't rely on
the array pointer during or after the foreach without
resetting it.
http://php.net/manual/en/control-structures.foreach.phpI've always found the foreach loop to a be a bit on the bizarre size, this is probably just another one of those bizarrities :) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP