note 36037 deleted from language.types.array by didou
| From: | didou@php.net | Date: | Wed, 24 Sep 2003 07:59:27 +0000 |
| Subject: | note 36037 deleted from language.types.array by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57028@lists.php.net to get a copy of this message | ||
Note Submitter: mastabog<a>hotmail<d>com
----
I don't know if this is a bug or normal behavior, and also if this was posted before,
nevertheless its worth remainding.
The thing is simple (and annoyinig in many situations): Array copying resets the internal pointer
!!!
---begin code---
$a = array(1,2,3,4,5,6,7);
next($a);
next($a);
echo current($a), "\n";
$b = $a;
echo current($a), "\n";
---end code---
It outputs:
3
1
Which means the internal pointer of array $a was reset when copying (which, if you think more, its
quite normal). This is quite annoying sometimes when you loop over an array, moving its internal
pointer (not with foreach), and inside the loop you wish to copy the array for a new loop for
example ... You either get infinit loop or stuck on the first element etc. What you should do is
copy to another array before starting the first loop (before moving the internal pointer).
Cheers,
Bogdan