ID:               34638
 Updated by:       [EMAIL PROTECTED]
 Reported By:      arborrow at jesuits dot net
-Status:           Open
+Status:           Bogus
 Bug Type:         Arrays related
 Operating System: WinXP Pro
 PHP Version:      5.0.5
 New Comment:

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);


Previous Comments:
------------------------------------------------------------------------

[2005-09-26 03:16:04] 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.


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=34638&edit=1

Reply via email to