note 36037 added to language.types.array

From: Date: Tue, 23 Sep 2003 18:54:19 +0000
Subject: note 36037 added to language.types.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-56985@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/language.types.array.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+36037 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+36037&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+36037&report=yes

« previous php.notes (#56985) next »