note 57159 deleted from function.array-search by felipe

From: Date: Sun, 10 Feb 2008 13:09:34 +0000
Subject: note 57159 deleted from function.array-search by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-135950@lists.php.net to get a copy of this message
Note Submitter: arborrow at jesuits dot net ---- I stumbled across some unexpected behavior with the array_search function. With a little help from the DallasPHPUsersGroup, I was able to trace it down to an array that I had attempted to start with a zero. The zero must be enclosed on single quote marks for the search function to work. Below are various syntaxes used to demonstrate the behavior. I hope this helps someone figure out why array_search is not working. Peace - Anthony <?php echo "<html><body>Let's try to search these arrays.<br><br>"; $findthis = "Assignments"; // syntax 1 $arcat1[] = 0; //comment out this line to test //$arcat1[] = '0'; //uncomment this line to test $arcat1[] = "Tests"; $arcat1[] = $findthis; $arcat1[] = "Quizzes"; $arcat1[] = "Participation"; $arcat1[] = 0; // the following search will not return the key if $arcat[] = 0; // it will return the if $arcat[] = '0'; interestingly enough it will also work if $arcat = 1; $keyresult = array_search ($findthis, $arcat1); print_r($arcat1); echo "<br>Arcat1 Key:".$keyresult."<br><br>"; //syntax 2 $arcat2 = array(0=>0, 1=>"Tests", 2=>"Quizzes", 3=>"Participation", 4=>"Assignments"); //comment out this line to test //$arcat2 = array(0=>'0', 1=>"Tests", 2=>"Quizzes", 3=>"Participation", 4=>"Assignments"); //uncomment this line to test // the following search will not return the key if $arcat2 = array(0=>0 ...); // it will return the key if $arcat2 = array(0=>'0' ...); $keyresult = array_search($findthis, $arcat2); print_r($arcat2); echo "<br>Arcat2 Key: ".$keyresult."<br><br>"; //syntax 3 $arcat3 = array(0); //comment out this line to test //$arcat3 = array('0'); //uncomment this line to test array_push($arcat3, $findthis); array_push($arcat3, "Tests"); array_push($arcat3, "Participation"); array_push($arcat3, "Quizzes"); // the following search will not return the key if $arcat3 = array(0); // it will return the key if $arcat3 = array('0'); interestingly enough it will also work if $arcat3 = array(1); $keyresult = array_search($findthis, $arcat3); print_r($arcat3); echo "<br>Arcat3 Key: ".$keyresult."<br><br>"; ?>

« previous php.notes (#135950) next »