|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77614 Array: mixed keys problem in foreach loop
Submitted: 2019-02-13 18:04 UTC Modified: 2019-02-14 04:45 UTC
From: shorvat at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: Irrelevant OS: Win 10 & Ubuntu server
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.
Block user comment
Status: Assign to:
Bug Type:
From: shorvat at gmail dot com
New email:
PHP Version: OS:


 [2019-02-13 18:04 UTC] shorvat at gmail dot com
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) {
    if($k == "word") {


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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-02-13 18:08 UTC]
This is because `"word" == 0` is true.

 [2019-02-13 18:14 UTC] shorvat at gmail dot com
That's right. But 'word' == '0' is false
 [2019-02-13 19:28 UTC]
-Status: Open +Status: Not a bug
 [2019-02-13 19:28 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at
> 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 20:55 UTC] spam2 at rhsoft dot net
> That's right. But 'word' == '0' is false

no it is not

$array['0'] becomes implicit $array[0]

php > echo (int)"word";
 [2019-02-14 04:45 UTC] shorvat at gmail dot com
I did not read documentation well enough apparently. Sorry.

At a time, when I reported this "bug", I thought that $array['0'] is not the same as $array[0].
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Sep 26 06:05:51 2022 UTC