php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42828 array internal pointer in foreach
Submitted: 2007-10-02 11:40 UTC Modified: 2007-10-02 12:20 UTC
From: ger dot kirill at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.2.4 OS: Windows, Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ger dot kirill at gmail dot com
New email:
PHP Version: OS:

 

 [2007-10-02 11:40 UTC] ger dot kirill at gmail dot com
Description:
------------
In this php version the internal array pointer after foreach is not always NULL even after the full cicle is made. Instead it points to the next key after the one which was current when the first change was made to the array we are currently iterating.

Reproduce code:
---------------
$ar  = array('val1', 'val2', 'val3');
var_dump(key($ar));
echo '<br>';
foreach ($ar as $key=>$val)
{	
	echo $key.'=>'.$val.'<br>';	
}
var_dump(key($ar));
echo '-------';
foreach ($ar as $key=>$val)
{	
	echo $key.'=>'.$val.'<br>';	
	$ar[2] = $ar[2];
}
var_dump(key($ar));
echo '-------';
foreach ($ar as $key=>$val)
{	
	echo $key.'=>'.$val.'<br>';	
	if ($key==1) $ar[2] = $ar[2];
}
var_dump(key($ar));

Expected result:
----------------
int(0)
0=>val1
1=>val2
2=>val3
NULL 
-------
0=>val1
1=>val2
2=>val3
NULL
-------
0=>val1
1=>val2
2=>val3
NULL

Actual result:
--------------
int(0)
0=>val1
1=>val2
2=>val3
NULL 
-------
0=>val1
1=>val2
2=>val3
int(1)
-------
0=>val1
1=>val2
2=>val3
int(2)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-02 12:20 UTC] tony2001@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 15 14:01:36 2024 UTC