|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 08 09:00:02 2026 UTC |
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