note 57159 added to function.array-search
| From: | arborrow at jesuits dot net | Date: | Sun, 25 Sep 2005 20:26:51 +0000 |
| Subject: | note 57159 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-95767@lists.php.net to get a copy of this message | ||
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>";
?>
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57159
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57159&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57159&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57159&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57159&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57159&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57159&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57159&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57159&report=yes
Search -- http://master.php.net/manage/user-notes.php