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
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: nicholas dot puleo at gmail dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 08 00:01:32 2025 UTC