Bug #66583 [Nab]: array_search always returns 0 for string elements for a mixed type array
| From: | y4lQBQPLLwxI1M9sbYf at gmail dot com | Date: | Mon, 27 Jan 2014 11:58:16 +0000 |
| Subject: | Bug #66583 [Nab]: array_search always returns 0 for string elements for a mixed type array | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-184029@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66583&edit=1
ID: 66583
User updated by: y4lQBQPLLwxI1M9sbYf at gmail dot com
Reported by: y4lQBQPLLwxI1M9sbYf at gmail dot com
Summary: array_search always returns 0 for string elements
for a mixed type array
Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: Linux
PHP Version: 5.5.8
Block user comment: N
Private report: N
New Comment:
Hello,
I agree that with the third argument, strict, set to TRUE, the array_search is able to return what I
expect.
But if I have an array like array("zero", "one", "two",
"three"), instead of array(0, 1, "two", 3), then, I don't have to set the
strict parameter to TRUE.
From the user perspective (which is mine), I don't follow the logic. So, yes, I did read the
array_search() documentation. But, in my case, the documentation didn't helped me. However, I
must admit, that I wasn't smart (or idiot) enough to say to myself: "hey! There is this
'strict' option here. Just give it try, even if it's not obvious that it's
sensible."
In other words, the documentation may be more explicit about what is strict and loose search cases.
Sorry for all inconveniences!
Thanks & regards,
Patrice
Previous Comments:
------------------------------------------------------------------------
[2014-01-27 06:39:48] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
array_search() does a loose comparison by default.
------------------------------------------------------------------------
[2014-01-27 06:37:27] y4lQBQPLLwxI1M9sbYf at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.array-search
---
I use PHP on RHEL 6:
$ php -v
PHP 5.3.3 (cli) (built: Dec 5 2013 07:09:40)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
But as, it's too old, I've tested in a Fedora 20:
$ php -v
PHP 5.5.8 (cli) (built: Jan 9 2014 08:33:30)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
In each case, I have the same behavior:
I create a mixed type array, containing both integers and strings. Then, I want to find the position
of one particular element, using array_search().
In the case the element is an integer, array_search() returns the correct answer. However, if the
element is a string, array_search() always returns 0, but not FALSE, really 0, the integer.
php > $ary = array(0, 1, "deux", 3);
php > $pos = array_search(3, $ary);
php > var_dump($pos);
int(3)
php > $pos = array_search("deux", $ary);
php > var_dump($pos);
int(0)
// here, IMHO, $pos should contains 2
// as below, with an array containing only strings
php > $ary = array("zero", "un", "deux", "trois");
php > $pos = array_search("deux", $ary);
php > var_dump($pos);
int(2)
Test script:
---------------
$ary = array(0, 1, "two", 3);
$pos = array_search("two", $ary);
if ( $pos === FALSE )
{
echo "The problem isn't in array_search()...";
exit;
}
if ( $pos == 2 )
{
echo "This is OK :-)".PHP_EOL;
}
else
{
echo "There is bug or a \"strange\" feature :-(".PHP_EOL;
}
Expected result:
----------------
$pos should contain 2
Actual result:
--------------
$pos contains 0
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66583&edit=1