php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63226 Mixed array index iteration has broken key=>value pair
Submitted: 2012-10-05 16:47 UTC Modified: 2012-10-05 17:13 UTC
From: nicholas dot puleo at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.4.7 OS: Ubuntu 12.0.4.1 LTS
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
42 - 42 = ?
Subscribe to this entry?

 
 [2012-10-05 16:47 UTC] nicholas dot puleo at gmail dot com
Description:
------------
If getting an array with mixed index types (in this case it was served from LDAP) 
- iterating over using a foreach loop yielded incorrect key values on the first 
changed index from associative to numerical.

Test script:
---------------
<?php
$d = array();
$d['text'] = 1;
$d[0] = 1;
$d[1] = 2;

foreach ($d as $k=>$v) {
	if ($k == "text") { 
		echo 'Should Only See Once';
		} else {
		echo $v;
		}
}
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-05 17:13 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2012-10-05 17:13 UTC] nikic@php.net
0 == "text", that's why you see it twice. If you want a strict string check use $k === "text".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 03:01:33 2024 UTC