php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62165 Array key bug
Submitted: 2012-05-26 10:14 UTC Modified: 2012-05-26 10:44 UTC
From: coreyavis at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: Irrelevant OS: linux
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: coreyavis at gmail dot com
New email:
PHP Version: OS:

 

 [2012-05-26 10:14 UTC] coreyavis at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.array-search
---
When using the array_search function to find the key of a value, and then making that key a variable, other functions don't recognize the key 0 (zero) as a valid variable.

I have PHP version 5.2.17 but was unable to upgrade cause I host with another company.

Test script:
---------------
$array1 = array('value1');
$array2 = array('value1');

for ($x=0; $x<1; $x++) {
   if (($key = array_search($array2[$x], $array1)) != false) {
      unset($array1[$key]);
   }
}

/* value1 should no longer be in array, but it is. */
print_r($array1);

Expected result:
----------------
$array1 should no longer contain 'value1'.
([0] => )

Actual result:
--------------
$array1 still contains 'value1'.
([0] => 'value1')

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-26 10:16 UTC] coreyavis at gmail dot com
-Operating System: Windows 7 +Operating System: linux
 [2012-05-26 10:16 UTC] coreyavis at gmail dot com
I put my home computer operating system, and not the host server operating system.
 [2012-05-26 10:44 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2012-05-26 10:44 UTC] rasmus@php.net
You need a strict check for FALSE there. Your array_search() call will correctly 
return the key which is 0, but 0 loosely evaluates to false in your loose != 
check there. Use !== instead to do a strict comparison. eg.

if (($key = array_search($array2[$x], $array1)) !== false)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 13:01:32 2025 UTC