php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34638 unexpected result from array_search
Submitted: 2005-09-26 03:16 UTC Modified: 2005-09-26 06:51 UTC
From: arborrow at jesuits dot net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.0.5 OS: WinXP Pro
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: arborrow at jesuits dot net
New email:
PHP Version: OS:

 

 [2005-09-26 03:16 UTC] arborrow at jesuits dot net
Description:
------------
array_search will return the value of the first improperly declared 0 value in the array rather than the correct result. This could be difficult for a programmer to track down; as it appears that the array_search is returning a result. Might there be a way for array_search to test the value first to prevent this from happening?

Reproduce code:
---------------
<?php
$findthis = "Assignments";
$arcat1[] = 0; //comment out this line to test
//$arcat1[] = '0'; //uncomment this line to test
$arcat1[] = "Tests";
//$arcat1[] = 0; //if this is uncommented it will falsely return a value of 2 (the key for the first non-quoted 0 value). 
// thus array_search seems to return the value of the first 0 found in the array regardless of what it is searching for I suspect the function is finding this and interpreting it as false causing the unusual behavior.
$arcat1[] = "Quizzes";
$arcat1[] = $findthis;
$arcat1[] = "Participation";
// the following search will not return the key if $arcat[] = 0;
// it will return the key if $arcat[] = '0'; interestingly enough it will also work if $arcat = 1;
$keyresult = array_search ($findthis, $arcat1);
print_r($arcat1); //the print_r result appears to be the same for $arcat[] = 0; and $arcat[] = '0';
echo "Arcat1 Key:".$keyresult;


Expected result:
----------------
I would expect array_search to return the key where the value it is searching for resides. I would not expect array_search to return the value of the first improperly declared 0 value. I have seen previously that this is not considered a bug; however, it is returning an unexpected result. If it is not corrected in the code, I would like to see a specific mention of it in the documentation.

Actual result:
--------------
Array_search returns the value of the first improperly declared 0 value. If the 0 is declared with single or double quotes the problem is avoided.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-09-26 06:51 UTC] marcot@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

There is nothing unexpected with this behaviour--you need to understand how PHP comparison operations work. The array_search() function will, by default, perform type-insensitive (==) comparisons between the needle and each value in the haystack. With this type of comparison, a string value is equal to an integer value of zero (just like a Boolean False would be). Thus, if you have a numeric integer element with a value of zero and search for a non-numeric string, that value will be returned (incidentally, it doesn't matter whether it's the first element of the array, just as long as it comes before the element you're looking for).

Note that this is different from having as an element of the array a string that contains the character '0'. In that case, PHP doesn't juggle the type of the needle to an integer, because it is comparing two strings and not a string and an integer. Therefore, the function works as expected. You can see how the integer and string cause the array to be different by using var_dump() instead of print_r().

In order for your script to work the way you intend it to, you need to pass a third Boolean True parameter to array_search(), which instructs the function to perform a type-sensitive search. Try changing your call to array_search() as follows:

$keyresult = array_search ($findthis, $arcat1, true);
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 22:01:27 2024 UTC