php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #13741 Cannot walk the same associative array twice in the same script
Submitted: 2001-10-19 06:19 UTC Modified: 2001-10-19 18:33 UTC
From: milo at cyberlifelabs dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.0.6 OS: FreeBSD 4.2-RELEASE
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: milo at cyberlifelabs dot com
New email:
PHP Version: OS:

 

 [2001-10-19 06:19 UTC] milo at cyberlifelabs dot com
I have a simple associative array of scalar name/value 
pairs -- specifically, the 50 US states and their 
abbreviations. The array is sorted using asort().

Two places in my script, I walk the array using the 
list/each syntax:

	while(list($abbr,$name) = each($STATES_HASH))
	{
		...
	}

The first time, everything works as it should. All 
subsequent times however (within the same script) the 
array acts as though it's blank. If I query the size using 
sizeof(), it reports the correct value. If I try to 
extract values from the array, it works fine. If I make a 
copy of the array to a new variable, the copy works 
properly (the first time).

It seems like there's a bug in the each() function. Looks 
almost as if every associative array has a pointer to the 
current element that's not getting reset when each() is 
done with it.

My configure line is as follows:

?'./configure' '--prefix=/usr/local/php-4.0.6' 
'--with-apache=../apache_1.3.19' 
'--with-pgsql=/usr/local/pgsql' '--enable-track-vars' 
'--with-config-file-path=/etc' 
'--with-jpeg-dir=/usr/local' '--with-tiff-dir=/usr/local' 
'--with-png-dir=/usr/local' '--with-zlib-dir=/usr/local' 
'--with-pdflib=/usr/local'


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-10-19 06:35 UTC] hholzgra@php.net
see http://php.net/reset
 [2001-10-19 06:38 UTC] hholzgra@php.net
... or read the documentation on each() itself:

"[...]
After each() has executed, the array cursor will be left on the next element of the array, or on the last element if it hits the end of the array
[...]"

 [2001-10-19 18:33 UTC] jeroen@php.net
And you should be using foreach anyway in this case.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jan 15 11:01:31 2025 UTC