Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Array Functions : array_search

array_search

Searches the array for a given value and returns the corresponding key if successful (PHP 4 >= 4.0.5, PHP 5)
mixed array_search ( mixed needle, array haystack [, bool strict] )

Searches haystack for needle and returns the key if it is found in the array, FALSE otherwise.

Note:

If needle is a string, the comparison is done in a case-sensitive manner.

Note:

Prior to PHP 4.2.0, array_search() returns NULL on failure instead of FALSE.

If the optional third parameter strict is set to TRUE then the array_search() will also check the types of the needle in the haystack.

If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead.

Example 275. array_search() example

<?php
$array
= array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
?>


Warning:

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

See also array_keys(), array_values(), array_key_exists(), and in_array().

Related Examples ( Source code ) » array_search


Code Examples / Notes » array_search

swoog dot news a@t laposte dot net_delme

[[Excuse me but the return line of the precedent fuction had an error :( :( :( ]]
Just For Say I Had Reworked the function Object_Search in Object_Search_All :
<?php
function object_search_all(&$needle, &$haystack, $strict=false) {
$results = array();
if(!is_array($haystack)) return NULL;
foreach($haystack as $k => $v) {
if(($strict && $needle===$v) || (!$strict && $needle==$v)) $results[] = $k;
}
return $results == array() ? false : $results;
}
?>
it's return _NULL_ if $haystack isn't an array;
it's retrun _false_ if there is no $needle in haystack
it work also with not numerics array (thanks to a _foreach_ )
Be Happy ;) ! :D
Bye !


joltmans

You can use array_search to simply parse a CLI program's input parameters such as:
[user@server ~]$ ./someCLIprog.php --username user1 --password 123password --option1 whatever
You can easily parse this information using the following code snippets:
<?php
  // Get the username
  $userkey = array_search("--username", $argv);
  // Make sure both that the token was found and followed by another valid token, AKA not one that begins with a -
  if ($userkey && !empty($argv[$userkey+1]) && strcmp($argv[$userkey+1]{0}, '-')) {
      $username = $argv[$userkey+1];
  }
  // If you want the token to be required add the following else statement
  else {
      exit("No username provided\n");
  }
  // repeat for each optional parameter,
?>


zwijntje82

use 3x === if you want to kill doubles in an array.
others will not work
if(array_search($twee[0],$y)===false){
echo $twee[0].array_search($twee[0],$y).'
';
}


otto

Unlimited depth array regular expression search, I found it useful, perhaps someone else will too. Searches on the array values only. Key search could be easily added.
function Array_Search_Preg( $find, $in_array, $keys_found=Array() )
{
if( is_array( $in_array ) )
{
foreach( $in_array as $key=> $val )
{
if( is_array( $val ) ) $this->Array_Search_Preg( $find, $val, $keys_found );
else
{
if( preg_match( '/'. $find .'/', $val ) ) $keys_found[] = $key;
}
}
return $keys_found;
}
return false;
}


ludvig dot ericson

To the reply below,
note that you can compare an array with another array using == and ===


axo

to php dot net at surfnation dot de
please make sure you read the warning on array_search - your multi-dimensional function will not return anything if the first element is the one you're looking for.


dmitry dot polushkin

To get the key of the found search value, use:
<?php
$a = array('a', 'b', 'c');
echo array_search(array_search('c', $a), array_keys($a));
?>


moz

To future proof against the 4.2.0 return change something like this should work:
<?php
       $key = array_search($needle, $haystack);
       if(is_null($key))
          $retval = false;
?>
and then test using:
<?php
       if($key !== false)
       {
               // $key exists in $haystack.
       }
?>


richgc

To expand on previous comments, here are some examples of
where using array_search within an IF statement can go
wrong when you want to use the array key thats returned.
Take the following two arrays you wish to search:
<?php
$fruit_array = array("apple", "pear", "orange");
$fruit_array = array("a" => "apple", "b" => "pear", "c" => "orange");
if ($i = array_search("apple", $fruit_array))
//PROBLEM: the first array returns a key of 0 and IF treats it as FALSE
if (is_numeric($i = array_search("apple", $fruit_array)))
//PROBLEM: works on numeric keys of the first array but fails on the second
if ($i = is_numeric(array_search("apple", $fruit_array)))
//PROBLEM: using the above in the wrong order causes $i to always equal 1
if ($i = array_search("apple", $fruit_array) !== FALSE)
//PROBLEM: explicit with no extra brackets causes $i to always equal 1
if (($i = array_search("apple", $fruit_array)) !== FALSE)
//YES: works on both arrays returning their keys
?>


ivan dot schmid

This is a simple Array-search & delete example.
============================================
<?PHP
//Array I am going to look in:
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
if(in_array('blue', $array))
{  
   unset($array[array_search('blue', $array)]); // $key = 0;
}
print_r($array);
/* Output:
Array
(
   [1] => red
   [2] => green
   [3] => red
)
*/
?>
============================================
Hope this helps someone.
Greetings Ivan


apolion

This is a silly thing, but it can drive you crazy if you do not read the warning above.
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('blue', $array); // $key = 2;
echo $key;
if($key==FALSE){
 echo "FALSE";
}else{
 echo "TRUE";
}
?>
It will print FALSE.
Use this coding instead:
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('blue', $array); // $key = 2;
//$key = array_search('red', $array);  // $key = 1;
echo $key;
if($key===FALSE){
 echo "FALSE";
}else{
 echo "TRUE";
}
?>
It will return TRUE


pornsak

This is a modified version of Mark Meves's wonderful function. I needed something that would be able to let me force search the key name where the needle should be found.
<?php
function array_search_recursive($needle, $haystack, $key_lookin="") {
$path = NULL;
if (!empty($key_lookin) && array_key_exists($key_lookin, $haystack) && $needle === $haystack[$key_lookin]) {
$path[] = $key_lookin;
} else {
foreach($haystack as $key => $val) {
if (is_scalar($val) && $val === $needle && empty($key_lookin)) {
$path[] = $key;
break;
}
elseif (is_array($val) && $path = array_search_recursive($needle, $val, $key_lookin)) {
array_unshift($path, $key);
break;
}
}
}
return $path;
}
?>


chrisadams

This has been noted but took me a while to figure out: When the array that you are searching starts with position '0' array_search returns '0' for a match - evaluating as FALSE:
$usernameArray[0]='moe';
$usernameArray[1]='larry';
$usernameArray[2]='curly';
$username='moe';
// broken
if ($userNameArrayPosition=array_search($username, $usernameArray))
{ // even though the expression is true, will eval as false
echo 'userNameArrayPosition -'.$userNameArrayPosition;
}else{
echo 'evaluates false - userNameArrayPosition = -'.$userNameArrayPosition;
}
// will echo 'evaluates false - userNameArrayPosition = - 0'
// works
if (is_numeric($userNameArrayPosition=array_search($username, $usernameArray))
{ // will be true for all numeric values, but not bool. false
echo 'userNameArrayPosition -'.$userNameArrayPosition;
}else{
echo 'evaluates false - userNameArrayPosition = -'.$userNameArrayPosition;
}
// will echo 'userNameArrayPosition  - 0'
$username='homer';
if (is_numeric($userNameArrayPosition=array_search($username, $usernameArray))
{ // will be true for all numeric values, but not bool. false
echo 'userNameArrayPosition -'.$userNameArrayPosition;
}else{
echo 'evaluates false - userNameArrayPosition = -'.$userNameArrayPosition;
}
// will echo 'evaluates false - userNameArrayPosition =  - FALSE'


ob

This function is based on the function in comment "array_search" from July 26th 2006.
I added the possibility of defining the key which $Needle shall be searched for.
<?php
// search haystack for needle and return an array of the key path,
// FALSE otherwise.
// if NeedleKey is given, return only for this key
// mixed ArraySearchRecursive(mixed Needle,array Haystack
//                            [,NeedleKey[,bool Strict[,array Path]]])
function ArraySearchRecursive($Needle,$Haystack,$NeedleKey="",
                             $Strict=false,$Path=array()) {
 if(!is_array($Haystack))
   return false;
 foreach($Haystack as $Key => $Val) {
   if(is_array($Val)&&
      $SubPath=ArraySearchRecursive($Needle,$Val,$NeedleKey,
                                    $Strict,$Path)) {
     $Path=array_merge($Path,Array($Key),$SubPath);
     return $Path;
   }
   elseif((!$Strict&&$Val==$Needle&&
           $Key==(strlen($NeedleKey)>0?$NeedleKey:$Key))||
           ($Strict&&$Val===$Needle&&
            $Key==(strlen($NeedleKey)>0?$NeedleKey:$Key))) {
     $Path[]=$Key;
     return $Path;
   }
 }
 return false;
}
?>
Remove unnecessary new lines. I had to add them because of too long lines.


evert_18

This function can search in multidimensional arrays, no mather how multidimensional the array is!
<?php
function array_search(&$array,$needle)
{
foreach($array as $key => $value)
{
if($value == $needle || $key == $needle)
return(true);
else
if(is_array($value))
$this->search($value,$needle);
else
return(false);
}
}
?>


gullevek

There were two previous entries for having a recursive search. The first one only searched for values, second one for values with an optional key.
But both of those stopped after they found an entry. I needed, that it searches recursive, with optional key and returns me all matches found in the array.
So I wrote this function:
needle is the value you search, haystack is the array of course, key is the optional key in the array where the needle should be. path should be never set on intial call. its an internal used variable.
It returns an array $path with the array entry 'found' where you can find all found groups. In these groups you have the array which holds the keys to find the data.
I hope this helps some of you.
<?php
   function array_search_recursive_all($needle, $haystack, $key, $path = NULL)
   {
       if (!$path['level'])
           $path['level'] = 0;
       if (!$path['work'])
           $path['work'] = array();
       if (!is_array($haystack))
           $haystack = array();
       // go through the array,
       foreach ($haystack as $_key => $_value)
       {
           // only value matches
           if (is_scalar($_value) && $_value == $needle && !$key)
           {
               $path['work'][$path['level']] = $_key;
               $path['found'][] = $path['work'];
           }
           // key and value matches
           elseif (is_scalar($_value) && $_value == $needle && $_key == $key)
           {
               $path['work'][$path['level']] = $_key;
               $path['found'][] = $path['work'];
           }
           elseif (is_array($_value))
           {
               // add position to working
               $path['work'][$path['level']] = $_key;
               // we will up a level
               $path['level'] += 1;
               // call recursive
               $path = array_search_recursive_all($needle, $_value, $key, $path);
           }
       }
       // cut all that is >= level
       array_splice($path['work'], $path['level']);
       // step back a level
       $path['level'] -= 1;
       return $path;
   }
?>
If you call it with this:
<?
   $right_side = array ('foo' => 'alpha', 'bar' => 'beta', 'delta' => 'gamma', 'gamma' => 'delta');
   $value = 'beta';
   $key = 'bar';
   $pos = array_search_recursive_all($value, $right_side, $key);
?>
You will find in $pos this data
<?
Array
(
   [level] => -1
   [work] => Array
       (
       )
   [found] => Array
       (
           [0] => Array
               (
                   [0] => bar
               )
       )
)
?>


voituk on asg dot kiev dot ua

There is no way to use this function to search an object in the array.
I want to suggest my own function for this problem solve
<?php
/** $Id$
* Searches the array for a given object and returns the corresponding key if successful or FALSE otherwise
*/
function object_search($needle, $haystack, $strict=false) {
if (!is_array($haystack)) return false;
for ($i=0; $i<count($haystack); ++$i) {
  if ($strict) {
   // STRICT
   if ((get_class($needle)==get_class($haystack[$i])) && ($needle==$haystack[$i])) return $i;
  } else {
   // NO STRICT
   if ($needle==$haystack[$i]) return $i;
  }
}
return false;
}// function object_search
?>


félix cloutier
There is no function to count the occurences of needle in haystack, so I made my own one...
<?php
function array_match($needle, $haystack)
{
if( !is_array($haystack) ) return false;

$i = 0;
while( (in_array( $needle, $haystack )) != FALSE )
{
$i++;
$haystack[array_search($needle, $haystack)] = md5($needle);
reset($haystack);
}

return $i;
}
?>
I know it's a bit crappy, but don't ask me too much, I'm still only 13... ;)


jcatchpoole

The warning above about the return value being NULL for versions prior to PHP 4.2.0 actually goes for 4.2.1 also - ie 4.2.1 appears to be returning NULL rather than FALSE on failure for me.

phil

The function posted by 'chen.avinadav@vbulletin.com' on '13-Apr-2002 04:15' is wrong!
You are matching the value not the key.  It should read:
<?php
function array_search($needle, $haystack) {
$match = false;
foreach ($haystack as $key => $value) {
if ($value == $needle) {
$match = $key;
}
}
return $match;
}
?>


csaba

The following function I created should have 15 lines of code from the first if to the last return (lines 1-2,6,8-12 start with if).
Csaba Gabor
<?php
function InsertPt ($ar, $val, $searchType=0, $startPos=0, $endPos=null) {
   // Finds the position where $val should be array_spliced into
   // subarray $ar[$startPos ... $endPos] to maintain the
   // sortedness of $ar (which entire array is assumed either
   // ascending or descending).
   // This position is unique unless $val equals an $ar element:
   //
   // In this case the insertion point is such that
   // insertion of $val would happen between the first
   // encountered $val in the subarray and the element
   // just prior to that, where the search is STARTING from
   // the smallest (!$searchType is true) or
   // largest (!!$searchType is true) side of the array
   //
   // $startPos, $endPos are restricted to
   // [-sizeof($ar),sizeof($ar)-1] and are interchangeable
   // UNLESS all elements are identical in which case it
   // establishes the ascending order.  If $ar is a singleton,
   // it is assumed ascending.
   // $endPos defaults to the end of the array
   // (0 if $startPos is negative, and
   // [the normal case] sizeof($ar)-1 otherwise).
   // Negative position values should be subtracted from
   // sizeof($ar) to get absolute position.
   // Logarithmic speed.  Works with strings.
   // Adaptable to case insensitive strings (7 comparisons)
   // Examples with $ar = array(2, 4, 5, 5, 5, 6, 7, 10, 12)
   //    InsertPt ($ar, 8)           => 7
   //    InsertPt ($ar, 5)           => 2  before first 5
   //    InsertPt ($ar, 5, 1)        => 5  after the last 5
   //    InsertPt ($ar, -3)          => 0  before the 2
   //    InsertPt ($ar, 12, 1)       => 9  after the 12
   // If $ar was reversed, the same five
   // functions calls would produce 2, 7, 4, 9, 0
   // If $ar = [6, 6, 6, 6, 6]
   //    InsertPt ($ar, 8)           => 5
   //    InsertPt ($ar, 6, 1, -1, 2) => 2
   //      that -1 translates to 4, which implies descending order
   if (!$ar || $startPos<-($aSize=sizeof($ar)) || $startPos>$aSize-1) return 0;
   if ($endPos===null) $endPos = 0 - ($startPos>=0); // default $endPos adjustment
   $startPos = mod(min($aSize-1, (max (-$aSize, $startPos))),$aSize); // translate to real start pos
   $endPos =   mod(min($aSize-1, (max (-$aSize, $endPos))),  $aSize); // translate to real end pos
   $diff = $endPos-$startPos;
   if (!($dir = ($ar[0]==$ar[$aSize-1] ? 0 : ($ar[$aSize-1]<$ar[0] ? 1 : -1)))) // constant series (-1 for ascending, 1 for descending)
return (($val<$ar[0] || ($val==$ar[0] && !$searchType)) ? $startPos + ($diff<0) : $endPos + !($diff<0));
   if ($diff<0) { $startPos+=$endPos; $endPos=$startPos-$endPos; $startPos-=$endPos; } // make sure $startPos <= $endPos
   if ($val<$ar[($dir<0 ? $startPos : $endPos)]) return ($dir<0 ? $startPos : $endPos+1); // These 2 lines for when $val falls outside
   if ($val>$ar[($dir<0 ? $endPos : $startPos)]) return ($dir<0 ? $endPos+1 : $startPos);      //      series speced by $startPos, $endPos
   if ($val==$ar[($tmp=(($dir<0)==!$searchType)) ? $startPos : $endPos]) return ($tmp ? $startPos : $endPos+1); // if $val at sensitive endpoint
   if (abs($diff)==1) return $endPos; // Double series (singleton was covered in 3 prior lines)
   $mid = floor(($endPos+$startPos)/2); // We will halve the series
   $tmp = ($dir<0)==(($val==$ar[$mid]) ? !$searchType : ($val<$ar[$mid])); // Decide which half we want
   return InsertPt ($ar, $val, $searchType, $tmp ? $startPos : $mid, $tmp ? $mid : $endPos);
}
function mod($num, $base) {
   // returns $num modulo $base within range [0..$base-1]
   return ($num - $base*floor($num/$base));
}
?>


johnjc-phpdocs

The === and !== are not fully documented in either the Comparison Operator, Booleans type sections. They are talked about a bit more in the sections on strpos() and array_search() but those sections refer you to the section on Booleans for further information.
I am putting my contribution on === and !== in the Booleans section with pointers to it from the comment areas of other sections.
http://uk.php.net/manual/en/language.types.boolean.php


jwhite

Searching arrays is a very slow process.  I've tried to use as many strings as I can for where an array is called, if possible. Here's a quick test to show the (eye-popping) difference between searching for a match between strings and arrays:
<?php
 $test_string = '';
 for ($i=1; $i <= 500000; $i++) {
   $test_string .= '['.$i.']';
 }
 $test_array = array();
 for ($i=1; $i <= 5000; $i++) {
   $test_array[] = $i;
 }
 $time_str = getmicrotime();
 for ($i=1; $i <= 500000; $i++) {
   strstr($i,$test_string);
 }
 $time_str = getmicrotime() - $time_str;
 $time_array = getmicrotime();
 for ($i=1; $i <= 5000; $i++) {
   array_search($i,$test_array);
 }
 $time_array = getmicrotime() - $time_array;
 echo "<H1>Test Results</H1>\r\n";
 echo "

String Test:&nbsp; $time_str seconds.\r\n";
 echo "

Array Test:&nbsp; $time_array seconds.\r\n";
  function getmicrotime(){
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
  }
?>
This code block takes some time to get going (because of the number of strings to place into memory), the results, even on modest hardware are staggering.  Comparing 500000 strstr() operations to 5000 array_search() operations on a P3-800 with 512 MB of RAM got the following output:
[H1]Test Results[/H1]
[P]String Test: 3.09137701988 seconds.[/P]
[P]Array Test:  4.23609495163 seconds.[/P]
The getmicrotime() function came from the note on microtime(), so that's not mine...credit to "daniel141 at yahoo dot com".
STRINGS RULE!
--
Justin White
YTZ Technical Services, LLC


congaz

Search a multi-dimensional array on keys!
-------------------------------------------
I needed to search dynamically in a multi-dimen array on keys. I came up with this little neat function. It is so amazingly simple, that I actually didn't think it would work - but it does...
mixed array_searchMultiOnKeys(array, array);
<?php
function array_searchMultiOnKeys($multiArray, $searchKeysArray) {
// Iterate through searchKeys, making $multiArray smaller and smaller.
foreach ($searchKeysArray as $keySearch) {
$multiArray = $multiArray[$keySearch];
$result = $multiArray;
}

// Check $result.
if (is_array($multiArray)) {
// An array was found at the end of the search. Return true.
$result = true;
}
else if ($result == '') {
// There was nothing found at the end of the search. Return false.
$result = false;
}
return $result;
// End of function,
}
// --- Test array_searchMultiOnKeys ---
$multiArray['webpages']['downloads']['music'] = 1;
$multiArray['webpages']['downloads']['pressmaterial'] = 5;
$multiArray['webpages']['links'] = 7;
array_searchMultiOnKeys($multiArray, array('webpages', 'links')); // returns 7.
array_searchMultiOnKeys($multiArray, array('webpages', 'downloads')); // returns true.
array_searchMultiOnKeys($multiArray, array('webpages', 'downloads', 'software')); // returns false.
?>
$multiArray / $searchKeysArray can be any size.
Happy hacking...


francois

Please note that, in PHP5, if you search for an object in an array using the array_search() function, PHP will return the first object whose properties match, not the same class and instance as your needle. In other words, the object comparison is of type '==', not '===' (see the 'Comparing objects' page).

rob

php dot net at surfnation dot de's function with a slight mod to correct for axo at axolander dot de's comments:
<?php
  function multiArraySearch($needle, $haystack){
    $value = false;
    $x = 0;
    foreach($haystack as $temp){
    $search = array_search($needle, $temp);
if (strlen($search) > 0 && $search >= 0){
    $value[0] = $x;
$value[1] = $search;
    }
    $x++;
    }
return $value;
 }
?>


radley25

Note to the previous post:
<?php
$map = array( 'version'    => 4
           , 'OS'         => 'Linux'
           , 'lang'       => 'english'
           , 'short_tags' => true
           );
 $key = array_search("4",$map);
  print $key;
//returns short_tags
//strange results..
$array = array (1,13, 20);
$key = array_search("13",$array);
print $key;
//works as expected
//returns 1
?>
If you put the number in quotes it searches for the array number.  Try it without the quotes.


stew

Not to labour the point too much, but here's another, slightly less verbose multi dimensional recursive function...
function array_key_exists_recursive($needle,$haystack) {
foreach($haystack as $key=>$val) {
  if(is_array($val)) { if(array_key_exists_recursive($needle,$val)) return 1; }
   elseif($val == $needle) return 1;
 }
 return 0;
}


digitally designed dot co dot uk

My Function to search a Multidimensional array.
Pass in :
$theNeedle as what you want to find.
$theHaystack as the array
$keyToSearch as what key in the array you want to find the value in.
<?
function myMulti_Array_Search($theNeedle, $theHaystack, $keyToSearch)
{
foreach($theHaystack as $theKey => $theValue)
{
$intCurrentKey = $theKey;

if($theValue[$keyToSearch] == $theNeedle)
{

return $intCurrentKey ;
}
else
{
return 0;
}
}
}
?>


erick dot xavier

Modifing the "multiarray_search" to unordered Array....
<?PHP
function multiarray_search($arrayVet, $campo, $valor){
   while(isset($arrayVet[key($arrayVet)])){
       if($arrayVet[key($arrayVet)][$campo] == $valor){
           return key($arrayVet);
       }
       next($arrayVet);
   }
   return -1;
}
//I.e.:
$myArr = array(
13 => array(
"fruit" => "banana"
),
654 => array(
"fruit" => "apple"
),
2445 => array(
"fruit" => "nothing more"
)
);
print(multiarray_search($myArr , "fruit", "apple"));
/*
Output:
654
*/
//and
print(multiarray_search($myArr , "fruit", "orange"));
/*
Output:
-1
*/
?>


hansen{ä}cointel.de

may be good to take note of PHP's mind-boggling 'fuzzy' (vulgo "magic type-casting") comparison features not only in using the results, but also in the search, too:
<?php
$a=array("a","b",0,"c","d");
echo "a: ".array_search("a",$a);
echo "b: ".array_search("b",$a);
echo "c: ".array_search("c",$a);
echo "d: ".array_search("d",$a);
echo "0: ".array_search("0",$a);
echo "x: ".array_search("x",$a);
echo "1: ".array_search("1",$a);
?>
will result in:
a: 0, b: 1, c: 2, d: 2, 0: 2, x: 2, 1: false
as from "c" on, the first match found in $a is "0", as any string compared to an int is automatically cast to (int)0.


bitmore.co.kr

Look Print
<?php
//original
//dennis dot decoene at removthis dot moveit dot be
//18-Jan-2004 12:41
function binsearch( $needle, $haystack ) {
$high = count($haystack);
$low = 0;
while ($high - $low > 1){
$probe = ($high + $low) / 2;
if ($haystack[$probe] < $needle) $low = $probe;
else $high = $probe;
print "high = $high,low = $low,probe = $probe,\$haystack[\$probe] = $haystack[$probe]
";
}
if ($high == count($haystack) || $haystack[$high] != $needle) return false;
else return $high;
}
//orginal
//Look bug
$arr=array(1,3,5,7,9,10,11,13);
$searchfor = 0;
print_r( binsearch($searchfor, $arr) );
//Print Show
//Á¤¼ö1À̳ª 0Àº ´ëÀÔµÈ °ª¿¡ ´ëÇÑ ¹è¿­ÀÇ 0À妽º¸¦ ºñ±³Çϴµ¥ ¹®Á¦°¡ ÀÖ´Ù.
// integer 1 or 0 bug Show look show
//high = 4,low = 0,probe = 4,$haystack[$probe] = 9
//high = 2,low = 0,probe = 2,$haystack[$probe] = 5
//high = 1,low = 0,probe = 1,$haystack[$probe] = 3
$searchfor = 1;
print_r( binsearch($searchfor, $arr) );
//Print Show
// integer 1 or 0 bug Show look show
//high = 4,low = 0,probe = 4,$haystack[$probe] = 9
//high = 2,low = 0,probe = 2,$haystack[$probe] = 5
//high = 1,low = 0,probe = 1,$haystack[$probe] = 3
$searchfor = 1;
print_r( binsearch($searchfor, $arr) );
//Print Show
//integer 3
//high = 4,low = 0,probe = 4,$haystack[$probe] = 9
//high = 2,low = 0,probe = 2,$haystack[$probe] = 5
//high = 1,low = 0,probe = 1,$haystack[$probe] = 3
//return 1
?>


dennis dot decoene

It has been said before: array_search is VERY slow. Everyone knows binary search is fast by design. Here is an implementation.
<?php
$arr=array(1,3,5,7,9,10,11,13);
$searchfor = 6;
echo binsearch($searchfor, $arr);
/**
* @return integer
* @param var $needle
* @param array $haystack
* @desc Feed a sorted array to $haystack and a value to search for to $needle.
It will return false if not found or the index where it was found.
This function is superfast. Try an array with 50.000 elements and search for something,
you will be amazed.
*/
function binsearch($needle, $haystack)
{
   $high = count($haystack);
   $low = 0;
   
   while ($high - $low > 1){
       $probe = ($high + $low) / 2;
       if ($haystack[$probe] < $needle){
           $low = $probe;
       }else{
           $high = $probe;
       }
   }
   if ($high == count($haystack) || $haystack[$high] != $needle) {
       return false;
   }else {
       return $high;
   }
}
?>


ludvig dot ericson

In response to the post beneath,
<?php
if ($userID = array_search($whatever, $array) !== false) {
}
?>
will also work, since it will be tested for explicitly NOT being a boolean false afterwards.


20-jan-2004 12:41

In response to dennis dot decoene:
Just remember that binary search requires the array to be sorted.  If it's not sorted (and you don't want to call sort() on it) then a binary search is not what you are lookng for.


saconner

In PHP versions before 4.2.0 needle was not allowed to be an array.  (funnily enough, at time of posting this note, we're still at ver 4.1.1 )

benoit

In answer to "info" at jake the spud dot com
This code doesn't work in all cases and may have unexpected results.
For example, it you have this code:
$array2search = array("pat","ter", "n");
if( strpos( implode( "", $array2search), "pattern" ) > -1 )
{
  // You will be there ... but "pattern" does not exist in the array ...
  // something is wrong !
}
There is no real solution using this code even if you try to enclose your elements by a special char ... because the special char could be part of the pattern.
If it works for you, it won't work in all cases.
/benoit


chappy

If you're searching for strings and you need a case-insensetive script, there's one:
function array_lsearch($str,$array){
$found=array();
foreach($array as $k=>$v){
if(strtolower($v)==strtolower($str)){
$found[]=$v;
}
}
$f=count($found);
if($f===0)return false;elseif($f===1)return $found[0];else return $found;
}
It returns the original string, not the lower. Also good if use strtoupper().


retestro_remove

If you're interested in finding a line in a file, after you read it into an array using file(), you can not use array_search since the match should be exact, and lines have line-endings ('\n', '\r' or '\n\r') - or else you know for sure what your lines contain physically.
The solution is to traverse the whole array, trim() each entry and then use array_search() - or - use something like the following small function I wrote for myself:
<?php
function search_array($needle, $haystack)
{
       if (!is_array($haystack) || !is_string($needle))
               return false;  // not valid argument types
       @reset($haystack);
       while (list ($key, $value) = each($haystack)) {
               $value = trim($value);  // remove spaces from the beginning and the end
               if ($value === $needle)
                       return $key;  // $needle was found, return the key
       }
       return false;  // no $needle was found in $haystack
}
?>
-----------------------------
Notes:
1. you should check the return value with === since 0 as a key equals to 'false'.
   i.e. if (search_array('my_line', $my_array) === false)  { ... }
2. I have no need in whitespace at the beginning of the line, therefore I use trim(). If it's important to you, use rtrim() or chop() instead.
-
- Sergey.


ryan

If you'd rather search for values in an array that match a regular expression, this function will do the trick (note that it returns the first index matched, not the last key matched as array_search does):
<?php
/**
 * Searches for a regex match in every value of a one-dimensional array.
 *
 * @param string $pattern POSIX-extended regular expression to match.
 * @param array $haystack Array to search in.
 * @return mixed Index of the first match, or FALSE if no match is found.
 */
function array_ereg($pattern, $haystack)
{
for($i = 0; $i < count($haystack); $i++)
{
if (ereg($pattern, $haystack[$i]))
return $i
}
return false;
}
?>


hanafi

If you want to search in a multi-dimensional array and just a portion, not the whole value, try this.
<?
$Projects[0] = array(123, "Text 1");
$Projects[1] = array(456, "Text 2");
$Projects[2] = array(789, "Text 3");
$search_value = "ext 2";
foreach ($Projects as $key => $row)
{
    foreach($row as $cell)
    {
       if (strpos($cell, $search_value) !== FALSE)
{
       echo  "

".$key;
}
    }
}
?>


fspillner

If you wanna find a value in multidimensional array with array_search. You should do the following:
Use For-Each, that it solves your Problem!
Example:
$map = array( array( Name=>'Jordan' ),
                   array( Name=>'Oneal' )
                 );
<?php
$search_value = "Jordan";
foreach ($map as $key => $row)
{
    foreach($row as $cell)
    {
         if ($cell == $search_value)
              print $key;
    }
}
?>


hakkierulez

If you simply want check if the search string is found, do this: (since the array number 0 evaluates as FALSE)
<?php
if (array_search($needle, $array)!== FALSE) {
  //code goes here (
}
?>


05-aug-2005 01:57

If you give a delimiter (or "glue") to implode() this because a little more robust.
To use previous example with slight modification:
$array2search = array("pat","ter","n");
$string = implode('|', $array2search); // now "pat|ter|n"
if (strpos($string, "pattern" ) !== false) {
  // wont get here
}
Now, obviously if you search for strpos($string, "pat|ter|n"), you're going to have problems again... But using a delimiter like this should help avoid most if not all "unforseen" cases.


php5 site builder

If you encounter a situation where condition test is failing on the result of either array_search or in_array, even when using "===" and "!==", make sure to set $strict = true in your array_search() or in_array() function call.
A situation such as :
$arTemp[0] = 1;
$arTemp[1] = 0;
$arTemp[2] = 3;
$arTemp[3] = 5;
$sTempTest = 'BLAH';
$bResult = in_array($sTempTest,$arTemp);
$bResult2 = array_search($sTempTest,$arTemp);
var_dump($bResult);
var_dump($bResult2);
will result in :
boolean true
int 1
Using :
$bResult = in_array($sTempTest,$arTemp,true);
$bResult2 = array_search($sTempTest,$arTemp,true);
will yield :
boolean false
boolean false
This is necessary in any instance where you have an array value equal to the integer zero.  As soon as you put the zero in quotes or double quotes (a string), the evaluation works with in_array & array_search without the $strict parameter being set.


"info"

If you don't need the power of recursive functions, and you want to just search within an array for the existence of a string pattern, you can always try this:
if( strpos( implode( "", $array2search), "pattern" ) > -1 )
{
   // code to execute here
}
It's kind of lame and childish, but gets the job done in a pinch.
Forgive me if someone else suggested this previously. I was racking my brain trying to do this using array_search(), in_array() and the like, and all I wanted was something dead simple.


cue

If you are using the result of array_search in a condition statement, make sure you use the === operator instead of == to test whether or not it found a match.  Otherwise, searching through an array with numeric indicies will result in index 0 always getting evaluated as false/null.  This nuance cost me a lot of time and sanity, so I hope this helps someone.  In case you don't know what I'm talking about, here's an example:
<?php
$code = array("a", "b", "a", "c", "a", "b", "b"); // infamous abacabb mortal kombat code :-P
// this is WRONG
while (($key = array_search("a", $code)) != NULL)
{
// infinite loop, regardless of the unset
unset($code[$key]);
}
// this is _RIGHT_
while (($key = array_search("a", $code)) !== NULL)
{
// loop will terminate
unset($code[$key]);
}
?>


dcsoboda

I've noticed problems with array_search() when it's handling extremely large arrays.
In one example, I had a 2000 slot array with a 128 char string in each slot, and was searching for a 128 char string within the array.
It regularly returned the wrong key. I even had it print the search string, along with the found key in the array, as a  test, and it would print obvious different strings.
The problem was alleviated when I ran gzcompress() on each array slot (and on my search string, obviously). In this case, no strings were longer than 67 bytes. It performed far faster and had no accuracy problems.


dens

I'm experiencing a weird search_array bug where the search works fine, but the first element in the array is ignored every time.  (e.g. the search works as if the first element doesn't even exist). Has anyone else run into this problem?
The only workaround I found was the put a dummy element into the first [0] spot into the array.  After that, everything works fine.
Strange, eh?


wouter van vliet
I was looking for a way to use a user defined function for array_search and eventually came up writing my own. Which was remarkably simple :P. Let me share this:
<?php
function array_usearch($cb, $ndl, $hs, $strict=false) {
if (!is_array($hs)) user_error('Third argument to array_usearch is expected to be an array, '.gettype($hs).' given', E_USER_ERROR);
foreach($hs as $key=>$value) if (call_user_func_array($cb, Array($ndl, $value, $key, $strict))) return $key;
};
?>
I'm not sure if I'm following correct conventions to specify the callback as the first argument, but it seemed most logical to me to not interrupt the order of the other four arguments (mixed needle, array haystack, boole strict).
[so far for my first post to the php notes]


mark dot php

I was going to complain bitterly about array_search() using zero-based indexes, but then I realized I should be using in_array() instead.
// if ( isset( $_GET['table'] ) and array_search( $_GET['table'], $valid_tables) ) {  // BAD: fails on first[0] element
// if ( isset( $_GET['table'] ) and ( FALSE !== array_search( $_GET['table'], $valid_tables) ) ) { OK: but wasteful and convoluted
  if ( isset( $_GET['table'] ) and in_array( $_GET['table'], $valid_tables) ) { // BETTER
The essence is this: if you really want to know the location of an element in an array, then use array_search, else if you only want to know whether that element exists, then use in_array()


darkvie

I wanted to search a  multidimensional array for a value & assign another element of array CONTAINING the searched value.
In the array I am using ($Projects), each item in $Projects contains a "Text_ID" & "Text_Value" field from a database result. I want to search for a "Text_ID" value (456 in this example)  & get it's "Text_Value" value assigned to a variable called $Text_Value.
Here's how I did it:
============================================
<?PHP
//Array I am going to look in:
$Projects[0] = array(123, "Text 1");  
$Projects[1] = array(456, "Text 2");  
$Projects[2] = array(789, "Text 3");  
// This loop goes through every element in $Projects
foreach ($Projects as $key => $ArrayRow)
{
// Now: $ArrayRow[0] = $Projects[x][0] and $ArrayRow[1] = $Projects[x][1]
// Look for the value "456".  Assign to variable & stop looking if found.
if ($ArrayRow[0] == "456")  {$Text_Value= $ArrayRow[1]; break; }
}
?>
============================================
Hope this helps someone.
-Darkive


softexpert

I use this for searching for a value in a bidimensional array .
<?php
function SearchBiDimArray(&$theArray, $dimNo, $searchValue, $returnIndex = true){
if(is_array($theArray)){
$keys = array_keys($theArray[0]);
$key = $keys[$dimNo];
$elcount = count($theArray);
for($i=0; $i < $elcount; $i++){
if($theArray[$i][$key] === $searchValue){
if ($returnIndex){
return $i;
}
else{
return $theArray[$i];
}
}
}
}
else{
return array_search($searchValue, $theArray);
}
}
$theArray = array();
$theArray[0]['firstproperty'] = 'avalue1';
$theArray[0]['secondproperty'] = 'anothervalue1';
$theArray[1]['firstproperty'] = 'avalue2';
$theArray[1]['secondproperty'] = 'anothervalue2';
$theArray[2]['firstproperty'] = 'avalue3';
$theArray[2]['secondproperty'] = 'anothervalue3';
print SearchBiDimArray($theArray, 1, 'anothervalue2', true);
// result is 1
print SearchBiDimArray($theArray, 1, 'anothervalue2', true);
// result is
//Array
//(
//    [firstproperty] => avalue2
//    [secondproperty] => anothervalue2
//)
?>


php

I think array_search uses serial search because they binary search function i wrote here seems to do a better job for records that are not always at the beginning of the array.
Here it is
<?php
function binarySearch ( $a, $t, $l, $r )
{
   if($t<$a[$l]||$t>$a[$r])return NULL;
   while ( $l < $r )
   {
       $m=intval($l+$r)/2;
       if($a[$m]==$t)return $m;
       elseif($t<$a[$m])$r=$m-1;
       elseif($t>$a[$m])$l = $m + 1;
   }
   if($t==$a[$r])
   return $r;
   return NULL;
}
?>
usage:
binarySearch ( array, target, left range, right range );
if your array is a multidimensional array, simply change the comparison method. :)


arborrow

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.
";
$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 "
Arcat1 Key:".$keyresult."
";
//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 "
Arcat2 Key: ".$keyresult."
";
//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 "
Arcat3 Key: ".$keyresult."
";
?>


vlad dot gladin

I found this useful especially for parsing htmls from
$file=file("http://a.website.com/htmlpage.html");
function array_search_extended($file,$str_search)
{
foreach($file as $line)
{
      if (strpos($line, $str_search)!== FALSE)
      {
             return $line;
      }
}
return false;
}


kmkz

I always wanted an array_replace function that could search and replace within an array, so I made one (its kinda simple and a bit inefficient, but c'est la vie):
<?php
function array_replace($search, $replace, &$array) {
 foreach($array as $key => $value) {
   if($value == $search) {
     $array[$key] = $replace;
   }
 }
}
?>


saltymeat

Here's how you can use array_search() to replace all occurances of a value in an array:
<?php
function array_replace($a, $tofind, $toreplace)
{
$i = array_search($tofind, $a);
if ($i === false)
{
return $a;
}
else
{
$a[$i] = $toreplace;
return array_replace($a, $tofind, $toreplace);
}
}
?>
Usage:
$a = array(1,2,3);
$a = array_replace($a, 1, 4);
echo $a[0]; // Outputs 4


ludwig_von_rocht

Here's a little function I wrote to find the key of the LAST occurrance of something in an array.
<?php
if(!function_exists('array_rsearch')){
function array_rsearch($search, $array, $strict = false){
$array = array_reverse($array, true);
foreach($array as $key => $value){
if($strict){
if($value === $search)
return $key;
} else {
if(strpos($value, $search))
return $key;
}
}
return false;
}
}
?>


php dot net

Here is my solution for a 2-dimensional search.
It combines the foreach with the faster array_search (I did use a recursive way, cause I am at war with it :) ).
it is quick, easy and (hopefully) very stable.
<?php
$map = array();
$map[1][1] = 11;
$map[2][1] = 21;
$map[3][1] = 31;
$map[1][3] = 13;
$varGesuchtesElement = 21;
$varGefundenesElement = fktMultiArraySearch($map,$varGesuchtesElement);
if ($varGefundenesElement){
print_r($varGefundenesElement);
}
else{
echo "Kein Element gefunden!";
}
function fktMultiArraySearch($arrInArray,$varSearchValue){

foreach ($arrInArray as $key => $row){
$ergebnis = array_search($varSearchValue, $row);

if ($ergebnis){
$arrReturnValue[0] = $key;
$arrReturnValue[1] = $ergebnis;
return $arrReturnValue;
}
}
}
?>


lars-magne

Further comments on the multidimensional array searches given earlier:
I needed an extended search function which could search in both keys and values in any # dimension array and return all results. Each result contains key/value hit, type (key or value), key path and value (in case result is a key).
<?php
function array_search_ext($arr, $search, $exact = true, $trav_keys = null)
{
 if(!is_array($arr) || !$search || ($trav_keys && !is_array($trav_keys))) return false;
 $res_arr = array();
 foreach($arr as $key => $val)
 {
   $used_keys = $trav_keys ? array_merge($trav_keys, array($key)) : array($key);
   if(($key === $search) || (!$exact && (strpos(strtolower($key), strtolower($search)) !== false))) $res_arr[] = array('type' => "key", 'hit' => $key, 'keys' => $used_keys, 'val' => $val);
   if(is_array($val) && ($children_res = array_search_ext($val, $search, $exact, $used_keys))) $res_arr = array_merge($res_arr, $children_res);
   else if(($val === $search) || (!$exact && (strpos(strtolower($val), strtolower($search)) !== false))) $res_arr[] = array('type' => "val", 'hit' => $val, 'keys' => $used_keys, 'val' => $val);
 }
 return $res_arr ? $res_arr : false;
}
// I.e.:
$haystack[754] = "Norwegian";
$haystack[28]['details']['Norway'] = "Oslo";
$needle = "Norw";
if($results = array_search_ext($haystack, $needle, false))
 foreach($results as $res)
   echo "Found '$needle' in $res[type] '$res[hit]', using key(s) '".implode("', '", $res['keys'])."'. (Value: $res[val])<br />\n";
/* Printed result will be:
Found 'Norw' in val 'Norwegian', using key(s) '754'. (Value: Norwegian)
Found 'Norw' in key 'Norway', using key(s) '28', 'details', 'Norway'. (Value: Oslo)
*/
?>


dakota

For versions later than 4.2.0, the check  isset($key) won't work properly because the function now returns false, which "isset". This change is missing in the Change Log!
So the right way to use the function is:
<?php
$key = array_search($needle, $array);
if ($key!==null&&$key!==false) {
 ...
}
?>
This example will work in both older and newer to 4.2.0 versions.
P.S.: My previous post where isset() is used won't work in newer versions.


leaetherstrip atnospam inbox dot ru

For get dennis dot decoene 's binary_search working on arrays with 1 or 2 elements, just replace
<?php
  while ($high - $low > 1){
?>
with
<?
  while ($high - $low >= 1){
?>


swoog dot news a@t laposte dot net_delme

for "voituk on asg dot kiev dot ua" :
no need to do :
(get_class($needle)==get_class($haystack[$i])) && ($needle==$haystack[$i])
for type sensible searches, Because, in PHP, two objects are equals if and only if they have same class and all of their attributes are the same (values).
But Because of the untyping of PHP, same attributes can be == but not === so, to avoid problems with type sensible searches, do :
$needle===$haystack[$i]
Is Better ! ;)


jupiter

Checks that array value STARTS with the string(needle), while other functions require an exact match OR the needle can be anywhere within.  This function can be manipulated to END with the needle if needed
<?php
// returns first key of haystackarray which array valuestring starts with needlestring, is case-sensitive
function arrayHaystackStartsWithNeedleString($haystackarray, $needlestring) {
if (is_array($haystackarray)) {  // confirms array
$needlelength = strlen($needlestring);  // length of string needle
foreach ($haystackarray as $arraykey => $arrayvalue) {  // gets array value
$arraypart = substr($arrayvalue, 0, $needlelength);  // first characters of array value
if ($needlestring == $arraypart) {  // did we find a match
return $arraykey;  // return will stop loop
}  // end match conditional
}  // end loop
}  // end array check
return false;  // no matches found if this far
}
?>
I haven't speed tested this, but it should be pretty quick.


swbrown

Be absolutely sure to check that your code that uses array_search now checks for 'false' too if you upgrade to PHP 4.2.0!
I was using array_search in my page authentication routines and this change had the fun side-effect of causing my code to always think a user had full permissions!  It was letting anyone click through to our installation of phpMyAdmin.  Not good indeed!


mjwilco

array_search() must match the whole value, not just a portion. To search for a portion, use:
<?php
$myarray = array("some apples", "some bannanas", "some oranges");
function array_search_bit($search, $array_in)
{
foreach ($array_in as $key => $value)
{
if (strpos($value, $search) !== FALSE)
return $key;
}

return FALSE;
}
$searchkey = array_search("oranges", $myarray); //old function, returns FALSE
$searchkey = array_search_bit("oranges", $myarray); //new function, returns 2
?>


chernyshevsky

array_search() in PHP 5 supports objects as needles whereas PHP 4 doesn't. The documentation should be updated to reflect this.

andrey

array_search() has kind of hidden behaviour which comes from the way PHP compares values of different types (PHP is a type-loose language) - so called type juggling.
for example :
<?php
$a=array(0,0,5,0,0);
var_dump(array_search(true, $a));
?>
In the array there are only integers but we give a boolean value TRUE for be the needle. The result is that array_search() returns the first non-negative value in the haystack array. The same way if we pass FALSE it will return the first value that compared with FALSE gives TRUE - for example NULL
<?php
$a=array(1,NULL,5,0,0);
var_dump(array_search(FALSE, $a));
?>
Returns:
int(1)  <-- the key of the NULL value


nospam

array_search won't accept objects as needle (see http://bugs.php.net/bug.php?id=20681) - a possible workaround is sth. like that:
<?php
function _array_search ($needle, $haystick) {
   foreach($haystick as $key => $val) {
       if ($needle === $val) {
           return($key);
       }
   }
   return(false);
?>
}
If you require 'bool strict' add e.g. ($strict) ? $needle === $val : $needle == $val


admin_espace

array search returns _a_ key not the first key. From my tests (linux-apache-php 4.1.2) it returns the last (though this may not be reliable).
i.e. the following all output 7
print_r(array_search(1,array(0,1,1,1,1,1,6,1,8,9),true));
print_r(array_search(1,array(0,1,2,3,4,5,6,1,8,9),true));
print_r(array_search(1,array(1,0,2,3,4,5,6,1,8,9),true));


kermes

A variation of previous searches that returns an array of keys that match the given value:
<?php
function array_ksearch($array, $str)
{
$result = array();
for($i = 0; $i < count($array); next($array), $i++)
if(strtolower(current($array)) == strtolower($str))
array_push($result, key($array);

return $result;
}
?>
Usage would be as follows:
<?php
$testArray = array('one' => 'test1', 'two' => 'test2', 'three' => 'test1', 'four' => 'test2', 'five' => 'test1');
print_r(array_ksearch($testArray, 'test1'));
?>


ben

A refinement on chappy at citromail dot hu's case-insensitive search function...
<?php
function array_isearch($str,$array){
foreach ($array as $k=>$v) {
if (strtolower($v) == strtolower($str)) { return $v; };
};
return false;
}
?>
Shall return false on no match or return the first value that matches if one or more matches are found (same functionality as the array_search() function).  It is also more efficient as it stops searching the array once a match is found.


mark meves

A quick-and-dirty array_search_all() that i used for a small
dup-checking routine.  
there are many, many ways to do something like this, not
the worst of which would be to use a relational database
for a dataset any larger than this ;)
-mark meves
<?php
/**
@return array of zero or more keys form $aHaystack whose
   values match $mScalarNeedle using a
   '==', (ie not strict) comparison
*/
function array_search_all($mScalarNeedle,$aHaystack){
   return array_keys( array_filter($aHaystack,
       create_function('$v','return $v == \''.addslashes($mScalarNeedle).'\';')
   ));
}
/*
test it:
*/
$aNicknames = array('jimmy'=>1,'james'=>1,'jim'=>1,
                   'billy'=>2,'william'=>2,'bill'=>2);
foreach(array('jim','bill') as $sName){
   echo "variations for \"$sName\" :(".
       implode(', ', array_search_all($aNicknames[$sName],$aNicknames)).
   ")\n";
}
/* outputs:
variations for "jim" are (jimmy, james, jim)
variations for "bill" are (billy, william, bill)
*/
?>


robertark

A better array_isearch would be to store all results in an array, then return the KEYS stored in $found, such as:
<?php
function array_isearch($str, $array){
 $found = array();
 foreach ($array as $k => $v)
     if (strtolower($v) == strtolower($str)) $found[] = $k;
 return $found;
}
?>
To use, simply have an array to search from then search it, for example:
<?php
function array_isearch($str, $array) {
 $found = array();
 foreach($array as $k => $v)
   if(strtolower($v) == strtolower($str)) $found[] = $k;
 return $found;
}
$stored = "these are an array";
$stored = explode(" ", $stored);
$compare = array("these", "are", "some", "results", "stored", "in", "an", "array");
foreach($stored as $store) {
 $results = array_isearch($store, $compare);
 foreach($results as $key => $result)
   echo "Key: ".$results[$key]."<br />Found: ".$compare[$result]."<br />";
}
?>
Hope this helps :-)
-Rob


pascal

@@ apolion at loliart dot com:
a thought to your example:
the $key you get from your array_search is "0" (0 => 'blue'). And as for me, 0 ist the same as FALSE, so your
<?php
if($key == false)
?>
hast to evaluate to true.
after that, with the '===' -operand, you additionally compare the type of the variable  and your statement evaluates to false, because 0 !== false.
so everything seems to be okay for me. =)


xurizaemon

@ ben, your array_isearch function returns the value, while array_search returns the key. to get equivalent (but case-insensitive) functionality, you would return $k not $v.

26-jul-2006 03:19

/**
* Searches haystack for needle and returns an array of the key path if it is found in the (multidimensional) array, FALSE otherwise.
*
* mixed array_searchRecursive ( mixed needle, array haystack [, bool strict[, array path]] )
*/
function array_searchRecursive( $needle, $haystack, $strict=false, $path=array() )
{
if( !is_array($haystack) ) {
return false;
}
foreach( $haystack as $key => $val ) {
if( is_array($val) && $subPath = array_searchRecursive($needle, $val, $strict, $path) ) {
$path = array_merge($path, array($key), $subPath);
return $path;
} elseif( (!$strict && $val == $needle) || ($strict && $val === $needle) ) {
$path[] = $key;
return $path;
}
}
return false;
}


scripts

* Multi-Dimensional Array Search *
If you're searching for a function to search in Multi-Arrays,
this is probably usefull for you.
-------------------------------------------------------------
<?php
function multi_array_search($search_value, $the_array)
{
if (is_array($the_array))
{
foreach ($the_array as $key => $value)
{
$result = multi_array_search($search_value, $value);
if (is_array($result))
{
$return = $result;
array_unshift($return, $key);
return $return;
}
elseif ($result == true)
{
$return[] = $key;
return $return;
}
}
return false;
}
else
{
if ($search_value == $the_array)
{
return true;
}
else return false;
}
}
?>
-------------------------------------------------------------
It will return an Array with the keys from the original array
where your search-string was found or false. e.g.:
-------------------------------------------------------------
<?php
$foo[1]['a']['xx'] = 'bar 1';
$foo[1]['b']['xx'] = 'bar 2';
$foo[2]['a']['bb'] = 'bar 3';
$foo[2]['a']['yy'] = 'bar 4';
$foo['info'][1] = 'bar 5';
$result = multi_array_search('bar 3', $foo);
print_r($result);
?>
-------------------------------------------------------------
Output:
Array
(
     [0] => 2
     [1] => a
     [2] => bb
)
-------------------------------------------------------------
I hope you like it ;)
greetz Udo
 


manicdepressive

<?php
/**
  recursively descend an arbitrarily deep multidimensional
  array, stopping at the first occurence of scalar $needle.
  return the path to $needle as an array (list) of keys
  if not found, return null.
  (will infinitely recurse on self-referential structures)
*/
function array_search_recursive( $needle, $haystack )
{
  $path = NULL;
  $keys = array_keys($haystack);
  while (!$path && (list($toss,$k)=each($keys))) {
     $v = $haystack[$k];
     if (is_scalar($v)) {
        if ($v===$needle) {
           $path = array($k);
        }
     } elseif (is_array($v)) {
        if ($path=array_search_recursive( $needle, $v )) {
           array_unshift($path,$k);
        }
     }
  }
  return $path;
}
?>
code till dawn!  -mark meves


richard

<?php
/**
* Search an array recursivly
*
* This function will search an array recursivly
* till it finds what it is looking for. An array
* within an array within an array within array
* is all good :-)
*
* @author Richard Sumilang <richard@richard-sumilang.com>
* @param string $needle What are you searching for?
* @param array $haystack What you want to search in
* @return boolean
* @access public
*/
function array_search_r($needle, $haystack){
foreach($haystack as $value){
if(is_array($value))
$match=array_search_r($needle, $value);
if($value==$needle)
$match=1;
if($match)
return 1;
}
return 0;
}
?>


elvenone

<?php
function array_reverse_search($value, $array) {
for($i = sizeof($array)-1; $i>=0; $i--) {
if ($array[$i] == $value) return $i;
}
return -1;
}
?>


Change Language


Follow Navioo On Twitter
array_change_key_case
array_chunk
array_combine
array_count_values
array_diff_assoc
array_diff_key
array_diff_uassoc
array_diff_ukey
array_diff
array_fill_keys
array_fill
array_filter
array_flip
array_intersect_assoc
array_intersect_key
array_intersect_uassoc
array_intersect_ukey
array_intersect
array_key_exists
array_keys
array_map
array_merge_recursive
array_merge
array_multisort
array_pad
array_pop
array_product
array_push
array_rand
array_reduce
array_reverse
array_search
array_shift
array_slice
array_splice
array_sum
array_udiff_assoc
array_udiff_uassoc
array_udiff
array_uintersect_assoc
array_uintersect_uassoc
array_uintersect
array_unique
array_unshift
array_values
array_walk_recursive
array_walk
array
arsort
asort
compact
count
current
each
end
extract
in_array
key
krsort
ksort
list
natcasesort
natsort
next
pos
prev
range
reset
rsort
shuffle
sizeof
sort
uasort
uksort
usort
eXTReMe Tracker