Bug #66583 [Opn->Nab]: array_search always returns 0 for string elements for a mixed type array

From: Date: Mon, 27 Jan 2014 06:39:48 +0000
Subject: Bug #66583 [Opn->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-184023@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 Updated by: requinix@php.net Reported by: y4lQBQPLLwxI1M9sbYf at gmail dot com Summary: array_search always returns 0 for string elements for a mixed type array -Status: Open +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: 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. Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#184023) next »