Bug #77614 [Com]: Array: mixed keys problem in foreach loop
| From: | shorvat at gmail dot com | Date: | Wed, 13 Feb 2019 18:14:27 +0000 |
| Subject: | Bug #77614 [Com]: Array: mixed keys problem in foreach loop | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-219561@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77614&edit=1
ID: 77614
Comment by: shorvat at gmail dot com
Reported by: shorvat at gmail dot com
Summary: Array: mixed keys problem in foreach loop
Status: Open
Type: Bug
Package: Arrays related
Operating System: Win 10 & Ubuntu server
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
That's right. But 'word' == '0' is false
Previous Comments:
------------------------------------------------------------------------
[2019-02-13 18:08:06] rtheunissen@php.net
This is because
"word" == 0 is true.
See https://3v4l.org/A66OG
------------------------------------------------------------------------
[2019-02-13 18:04:16] shorvat at gmail dot com
Description:
------------
PHP 7.0.10 & 5.6.25
Key comparation in foreach loop not working as expected when keys are of mixed types (strings and
ints)
If I use triple-equals operator (===) it works just fine, but it seems it should work with
double-equals operator (==) as well.
Test script:
---------------
$array = array();
$array['word'] = "test 1";
$array['0'] = "test 2";
$array['1'] = "test 3";
foreach($array as $k=>$v) {
var_dump($k);
if($k == "word") {
continue;
}
print_r($v);
}
Expected result:
----------------
C:\wamp64\www\tst\array_bug.php:10:string 'word' (length=4)
C:\wamp64\www\tst\array_bug.php:10:int 0
test 2
C:\wamp64\www\tst\array_bug.php:10:int 1
test 3
Actual result:
--------------
C:\wamp64\www\tst\array_bug.php:10:string 'word' (length=4)
C:\wamp64\www\tst\array_bug.php:10:int 0
C:\wamp64\www\tst\array_bug.php:10:int 1
test 3
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77614&edit=1