Bug #66583 [NEW]: array_search always returns 0 for string elements for a mixed type array

From: Date: Mon, 27 Jan 2014 06:37:27 +0000
Subject: Bug #66583 [NEW]: array_search always returns 0 for string elements for a mixed type array
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-184022@lists.php.net to get a copy of this message
From: y4lQBQPLLwxI1M9sbYf at gmail dot com Operating system: Linux PHP version: 5.5.8 Package: Arrays related Bug Type: Bug Bug description:array_search always returns 0 for string elements for a mixed type array 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 bug report at https://bugs.php.net/bug.php?id=66583&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=66583&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=66583&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=66583&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=66583&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=66583&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=66583&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=66583&r=needscript Try newer version: https://bugs.php.net/fix.php?id=66583&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=66583&r=support Expected behavior: https://bugs.php.net/fix.php?id=66583&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=66583&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=66583&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=66583&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66583&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=66583&r=dst IIS Stability: https://bugs.php.net/fix.php?id=66583&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=66583&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=66583&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=66583&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=66583&r=mysqlcfg

« previous php.bugs (#184022) next »