php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66583 array_search always returns 0 for string elements for a mixed type array
Submitted: 2014-01-27 06:37 UTC Modified: 2014-01-27 11:58 UTC
From: y4lQBQPLLwxI1M9sbYf at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.5.8 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: y4lQBQPLLwxI1M9sbYf at gmail dot com
New email:
PHP Version: OS:

 

 [2014-01-27 06:37 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-01-27 06:39 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-01-27 06:39 UTC] 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 11:58 UTC] y4lQBQPLLwxI1M9sbYf at gmail dot com
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jun 18 00:01:32 2024 UTC