Bug #77614 [Com]: Array: mixed keys problem in foreach loop
| From: | spam2 at rhsoft dot net | Date: | Wed, 13 Feb 2019 20:55:25 +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-219566@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: spam2 at rhsoft dot net
Reported by: shorvat at gmail dot com
Summary: Array: mixed keys problem in foreach loop
Status: Not a bug
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
no it is not
$array['0'] becomes implicit $array[0]
php > echo (int)"word";
0
http://php.net/manual/en/language.types.type-juggling.php
http://php.net/manual/en/types.comparisons.php
Previous Comments:
------------------------------------------------------------------------
[2019-02-13 19:28:46] requinix@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
http://php.net/manual/en/language.types.array.php
> Additionally the following key casts will occur:
>
> * Strings containing valid decimal integers, unless the number is preceded by a
> + sign, will be cast to the integer type. E.g. the key "8" will actually be
> stored under 8. On the other hand "08" will not be cast, as it isn't a valid
> decimal integer.
As the var_dump shows, $k === 0.
------------------------------------------------------------------------
[2019-02-13 18:14:27] shorvat at gmail dot com
That's right. But 'word' == '0' is false
------------------------------------------------------------------------
[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