Bug #62165 [Opn->Nab]: Array key bug
| From: | rasmus@php.net | Date: | Sat, 26 May 2012 10:44:40 +0000 |
| Subject: | Bug #62165 [Opn->Nab]: Array key bug | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-170843@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62165&edit=1
ID: 62165
Updated by: rasmus@php.net
Reported by: coreyavis at gmail dot com
Summary: Array key bug
-Status: Open
+Status: Not a bug
Type: Bug
Package: Unknown/Other Function
Operating System: linux
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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)
Previous Comments:
------------------------------------------------------------------------
[2012-05-26 10:16:31] coreyavis at gmail dot com
I put my home computer operating system, and not the host server operating system.
------------------------------------------------------------------------
[2012-05-26 10:14:53] 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')
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62165&edit=1