数组 函数
在线手册:中文 英文
PHP手册


数组 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: 在数组中搜索给定的值,如果成功则返回相应的键名

用户评论:

sneskid at hotmail dot com (16-Mar-2012 04:50)

I had an array of arrays and needed to find the key of an element by comparing actual reference.
Beware that even with strict equality (===) php will equate arrays via their elements recursively, not by a simple internal pointer check as with class objects. The === can be slow for massive arrays and also crash if they contain circular references.

This function performs reference sniffing in order to return the key for an element that is exactly a reference of needle.

<?php
function array_ref_search(&$v, array &$s)
{
    if(
is_object($v)){ return array_search($v, $s, true); }
    foreach(
$s as $rK => &$rV)
    {
// reference sniff
       
$tV = $v;
        if( (
$rV === ($v = 1)) && ($rV === ($v = 0)) ){
       
$v = $tV; return $rK; }
       
$v = $tV;
    }
    return
false; // use null for php < 4.2.0
}

$list   = array();
$list['A'] = &$valA; $list['B'] = &$valB;

$valA = 1; $valB = 1;
echo
'array_ref_search: ', array_ref_search($valB, $list), '</br>'; // key 'B'
echo 'array_search:     ', array_search($valB, $list, true), '</br>'; // key 'A'

$valA = array(1,2,3); $valB = array(1,2,3);
echo
'array_ref_search: ', array_ref_search($valB, $list), '</br>'; // key 'B'
echo 'array_search:     ', array_search($valB, $list, true), '</br>'; // key 'A' because ($valA === $valB) is true by elements

$valB[] = &$valB; // circular reference
echo 'array_ref_search: ', array_ref_search($valB, $list), '</br>'; // key 'B'
echo 'array_search:     ', array_search($valB, $list, true), '</br>'; // crash because ($valB === $valB) causes infinite loop
?>

nateferrero at gmail dot com (28-Jan-2012 12:56)

I built a function to find the numeric index of a key in an array.

<?php

/**
 * Array key index
 * @author Nate Ferrero
 */
function array_key_index(&$arr, $key) {
   
$i = 0;
    foreach(
array_keys($arr) as $k) {
        if(
$k == $key) return $i;
       
$i++;
    }
}

nsstl (23-Jan-2012 05:57)

Some thing that I wrote from a while, for what ever usefull purpose

This retrieves recursively a value from an array using a path-like notation.
 
   with:
     $delimiter         path delimiter
     $strict              if false, the search will allow gaps between path's components        

<?php
function path_through_array($path, $array, $delimiter = '.', $strict = false)
{
 
$path_token = explode($delimiter, $path);
 
$head = array_shift($path_token);

  if (isset(
$array[$head]) && (0 == count($path_token)))
  {
    return
$array[$head];
  }
  else if (isset(
$array[$head]))
  {
    return
path_through_array(implode($delimiter, $path_token), $array[$head], $delimiter, $strict);
  }
  else if (
$strict == true)
  {
    return
false;
  }

  foreach (
$array as $key=>$value)
  {
    if (
is_array($value))
    {
     
$found = path_through_array($path, $value, $delimiter, $strict);

      if(
false != $found)
      {
        return
$found;
      }
    }
  }
  return
false;
}

//use:

$array1 = array('path' => array('to' => array('get' => array('something' => 'bar')), 'somethingelse'), 'foo' => 'anotherthing');

$val1 = path_through_array('path.to.get.something', $array1);                         // => will return : 'bar'
$val2 = path_through_array('path.to.get', $array1);                                       // => will return : array('something'=>'bar')
$val3 = path_through_array('path/to/get/something', $array1, '/');                  // => will return : 'bar'
$val4 = path_through_array('path.to.something', $array1);                            // => will return : 'bar'
$val5 = path_through_array('path.to.something', $array1, '.', true);               // => will return : false

Ouz zcan (mail at oguzozcan dot com) (17-Dec-2011 10:06)

This is a multidimensional recursive find function :

<?php
function GetArrKey( $findArr, $key_arr, $depth=0 )
{
        if(
count($key_arr) <= $depth || !array_key_exists($key_arr[$depth], $findArr) )
                return
NULL;
        else if(
count($key_arr) == $depth+1 )
                return
$findArr[$key_arr[$depth]];
       
        return
self::GetArrKey( $findArr[$key_arr[$depth]], $key_arr, $depth+1 );
}
?>

Using :
<?php
        $findArray
= array( "first_dim" => array( "first_dim_in_1" => array( "first_dim_in_1_val_1", "first_dim_in_1_val_2", "first_dim_in_1_val_3" ), "first_dim_in_2" => array( "first_dim_in_2_val_1", "first_dim_in_2_val_2", "first_dim_in_2_val_3" ) ), "second_dim" => array( "second_dim_in" => "test", "second_dim_in_val_1"));

       
var_dump( GetArrKey( $findArray, array( "first_dim", "first_dim_in_2" ) ) );

       
/*
            Prints :
            array( "first_dim_in_2_val_1", "first_dim_in_2_val_2", "first_dim_in_2_val_3" )
        */

        //----------------------------------------------------//

       
var_dump(GetArrKey( $findArray, array( "first_dim", "first_dim_in_2", 0 ) ) );

       
/*
            Prints :
            string(20) => "first_dim_in_2_val_1"
        */

        //----------------------------------------------------//

       
var_dump(GetArrKey( $findArray, array( "second_dim", 0 ) ) );

       
/*
            Prints :
            string(4) => "test"
        */

        //----------------------------------------------------//

       
var_dump(GetArrKey( $findArray, array( "second_dim", 0 ) ) );

       
/*
            Prints :
            string(4) => "test"
        */

        //----------------------------------------------------//

       
var_dump(GetArrKey( $findArray, array( "second_dim", 1 ) ) );

       
/*
            Prints :
            string(19) => "second_dim_in_val_1"
        */

        //----------------------------------------------------//

       
var_dump(GetArrKey( $findArray, array( "second_dim", 1, 1 ) ) );

       
/*
            Prints :
            std_object => NULL
        */

?>

revoke (11-Oct-2011 12:12)

Better solution of multidimensional searching.

<?php
function multidimensional_search($parents, $searched) {
  if (empty(
$searched) || empty($parents)) {
    return
false;
  }
 
  foreach (
$parents as $key => $value) {
   
$exists = true;
    foreach (
$searched as $skey => $svalue) {
     
$exists = ($exists && IsSet($parents[$key][$skey]) && $parents[$key][$skey] == $svalue);
    }
    if(
$exists){ return $key; }
  }
 
  return
false;
}

$parents = array();
$parents[] = array('date'=>1320883200, 'uid'=>3);
$parents[] = array('date'=>1320883200, 'uid'=>5);
$parents[] = array('date'=>1318204800, 'uid'=>5);

echo
multidimensional_search($parents, array('date'=>1320883200, 'uid'=>5)); // 1
?>

stooshie at gmail dot com (06-Oct-2011 10:51)

Example of a recursive binary search that returns the index rather than boolean.
<?php
// returns the index of needle in haystack
function binSearch($needle, $haystack)
{
   
// n is only needed if counting depth of search
   
global $n;
   
$n++;
   
// get the length of passed array
   
$l = count($haystack);
   
// if length is 0, problem
   
if($l <= 0)
    {
        return -
1;
    }
   
// get the mid element
   
$m = (($l+($l%2))/2);
   
// if mid >= length (e.g. l=1)
   
if($m >= $l)
    {
       
$m = $m-1;
    }
   
// get the indexed element to compare to the passed element and branch accordingly
   
$compare = $haystack[$m];
    switch(
true)
    {
        case(
$compare>$needle):
        {
           
// recurse on the lower half
           
$new_haystack = array_slice($haystack, 0, $m);
           
$c = count($new_haystack);
           
$r = binSearch($needle, $new_haystack);
           
// return current index - (length of lower half - found index in lower half)
           
return $m - ($c - $r);
            break;
        }
        case(
$compare<$needle):
        {
           
// recurse on the upper half
           
$new_haystack = array_slice($haystack, $m, ($l-$m));
           
$c = count($new_haystack);
           
$r = binSearch($needle, $new_haystack);
           
// return current position + found index in upper half
           
return $m + $r;
            break;
        }
        case(
$compare==$needle):
        {
           
// found it, so return index
           
return $m;
            break;
        }
    }
}
?>

Dave C (27-Sep-2011 09:18)

Here is a version of binary search that is done via recursion instead of iteration.  Remember that your data needs to be presorted!

<?php
static function Bin_Search(&$needle, &$haystack, $start, $end) {
        if(
$end < $start)
        {
            return
false;
        }

       
$mid = (int)(($end - $start) / 2) + $start;
 
        if(
$haystack[$mid] > $needle)
        {
            return
Bin_Search($needle, $haystack, $start, $mid - 1);
        }
        else if(
$haystack[$mid] < $needle)
        {
            return
Bin_Search($needle, $haystack, $mid + 1, $end);
        }
        else
        {
            return
true;
        }
}
?>

Jordan Rutty (ruttyj92[-at-][hotmailcom) (04-Jul-2011 06:41)

Array AFTER/BEFORE Function

function made to mimic the jQuery function to insert items/arrays into another array before or after a specific key. It is split into two functions for reuse to find the offset of a key. it detects if the array is associative (can also be manually controlled) and adds the keys where needed. if the inserted array is associative then any overlapping keys will be replaced by the new ones.

array_insert(array &$original , mixed $insert=null , var $position='after' , $key=null [, $keep_keys=true])

options for $position = (after|before)

<?php
function key_offset(&$array, $searchKey){
    if(
is_array($array) && array_key_exists($searchKey, $array)){
       
$counter = 0;
        foreach(
$array as $key => $value){
            if(
$searchKey == $key){
                return
$counter;
            } else {
               
$counter++;
            }
        }
    }
}

function
array_insert(&$original, $insert=null, $position='after', $key=null, $sens=true){
    if(!empty(
$insert)){   
          
$after = $position == 'after' ? true : false;
          
$insert = (array)$insert;          
          
$assoc = (array_keys($insert) !== range(0, count($insert)-1));          
          
$keep_keys = is_string($insert) || !$assoc || $sens;
          
        if(!empty(
$key) && array_key_exists($key, $original)){   
           
$start = key_offset($original, $key);
              
$start = $after ? $start+1 : $start;
            if(!
$keep_keys){   
               
array_Splice($original,$start,0,$insert);
                } else {
               
$keys = array_Keys($original);
               
$values = array_Values($original);
               
$insert = (array)$insert;
               
$rKeys = array_Keys($insert);
               
$rValues = array_Values($insert);
               
array_Splice($keys,$start,0,$rKeys);
               
array_Splice($values,$start,0,$rValues);
                   
$original = array_Combine($keys,$values);
                }
        } else {
           
$original = $after ? array_merge($original, $insert) : array_merge($insert, $original);
        }
    }
}

$insert = array('omg'=> 'it works', 'this_is' => 'awsome', 'r' =>103,104);
$insert2 = range(11,15);
$insert3 = array(100=>101,102,103,104);

$original=range(1,10);
array_insert($original, $insert, 'after', 4);

$original=range(1,10);
array_insert($original, $insert, 'after', 4, false);

$original=range(1,10);
array_insert($original, $insert2, 'after', 4);

$original=range(1,10);
array_insert($original, $insert3, 'after', 4);

/*
Array
(
    [0] => 104
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [omg] => it works
    [this_is] => awsome
    [r] => 103
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => it works
    [6] => awsome
    [7] => 103
    [8] => 104
    [9] => 6
    [10] => 7
    [11] => 8
    [12] => 9
    [13] => 10
)

Array
(
    [0] => 11
    [1] => 12
    [2] => 13
    [3] => 14
    [4] => 15
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [100] => 101
    [101] => 102
    [102] => 103
    [103] => 104
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)
*/
?>

willemvannus at gmail dot com (11-Feb-2011 11:23)

I found a simple script to remove certain values from a array.
I use it when using the scandir function, because i don't want certain items in the array (like the ., .. folders and the thumbs.db)

<?php
$music
= scandir("music");
unset(
$music[array_search('.',$music)]);  // Removing the . directory
unset($music[array_search('..',$music)]); // Removing the .. directory
unset($music[array_search('Thumbs.db',$music)]); // Removing the Thumbs.db file
unset($music[array_search('thumbs.db',$music)]); // Removing the thumbs.db file
?>

syrgjyn at gmail dot com (19-Jan-2011 03:51)

Recently I had to get all locations of a one-dimensional non-associative array (needle, not array of needles) in another one-dimensional non-associative array (haystack) without using more memory than necessary. I haven't found any satisfactory way to do that, so I wrote the following function, inspired by the array_slice() source code.

Notes:
0. $offset, $length and $strict behave the same way as those for array_slice() and array_search();
1. this function doesn't use foreach or array_slice(), neither does it modify $needle or $haystack, so no temporary copies are needed/created, regardless of whether the arguments are passed by reference or not -- useful when dealing with very large haystacks and needles;
2. $needle and $haystack are typecast to array if they're not arrays;
3. you can specify the $start position to start searching from in the $haystack (default is 0);
4. negative $start values can be used if you want to specify the starting position from the end of $haystack.
5. you can specify the search $length, starting from $start; a negative value stops the search at that many elements from the end of the haystack;
6. pass true as the fifth argument to do a strict search (not tested on objects);
7. the function returns an array if it finds one or more occurrences of $needle in the $haystack, false on failure.
8. keys are preserved;
9. if you want to get only the first location of $needle in $haystack, returned as an integer rather than array -- in other words, to make an array_match() function instead of array_match_all() -- remove this line:

  $results = array();

and replace:

    if ($found)
    {
      $results[] = $i;
      $i += $ncount - 1;
    }
  }
  if (!empty($results))
    return $results;
  else
    return false;

with:

    if ($found)
      return $i;
  }
  return false;

And now the function:

<?php
function array_match_all($needle, $haystack, $offset = 0, $length = 0, $strict = false)
{
  if (!
is_array($needle))
   
$needle = (array) $needle;
  if (!
is_array($haystack))
   
$haystack = (array) $haystack;

 
$offset = (int) $offset;
 
$length = (int) $length;
 
$strict = ($strict != false);
 
$ncount = count($needle);
 
$hcount = count($haystack);

 
// Empty arrays? Really? Also, no point in dealing with a needle larger than the haystack.
 
if (($hcount == 0) || ($ncount == 0) || ($ncount > $hcount))
    return
false;

 
// Is offset beyond the upper bound?
 
if ($offset > $hcount - $ncount)
    return
false;
 
// If offset is specified from the end of haystack, is it beyond the lower bound?
 
elseif ($offset < 0 && ($offset = $hcount + $offset) < 0)
   
$offset = 0;

 
// Determine the length.
 
if ($length == 0)
   
$length = $hcount;

  if (
$length < 0)
   
$length = $hcount - $offset + $length;
  elseif (
$offset + $length > $hcount)
   
$length = $hcount - $offset;

 
// Is the needle larger than the portion of the haystack we are searching in?
 
if (($length <= 1) || ($ncount > $length))
    return
false;

 
// Determine the actual search length.
 
$length = $offset + $length - $ncount + 1;

 
$results = array();
  for (
$i = $offset; $i < $length; ++$i)
  {
   
$found = true;
    for (
$j = 0; $j < $ncount; ++$j)
    {
      if ((
$strict && ($haystack[$i + $j] !== $needle[$j])) || (!$strict && ($haystack[$i + $j] != $needle[$j])))
      {
       
$found = false;       
        break;
      }
    }
    if (
$found)
    {
     
$results[] = $i;
     
$i += $ncount - 1;
    }
  }

  if (empty(
$results))
    return
false;
  else
    return
$results;
}
?>

stefano@takys dot it (14-Jan-2011 09:58)

for searching case insensitive better this:

<?php
array_search
(strtolower($element),array_map('strtolower',$array));
?>

user at www dot mp3s dot pl (28-Dec-2010 09:25)

for case insensitive array_search you could use:

<?php
function array_search_i($str,$array){
    foreach(
$array as $key => $value) {
        if(
stristr($str,$value)) return $key;
    }
    return
false;
}
?>

manchimen [at) yahoo [dot) com (01-Nov-2010 10:18)

Hey all - I needed a function to search for an array in a multi-dimensional array, but only the keys I wanted, not all keys. Hee if my function:

<?php
function my_array_search($needle, $haystack) {
        if (empty(
$needle) || empty($haystack)) {
            return
false;
        }
       
        foreach (
$haystack as $key => $value) {
           
$exists = 0;
            foreach (
$needle as $nkey => $nvalue) {
                if (!empty(
$value[$nkey]) && $value[$nkey] == $nvalue) {
                   
$exists = 1;
                } else {
                   
$exists = 0;
                }
            }
            if (
$exists) return $key;
        }
       
        return
false;
    }
?>

Examples:
<?php
$needle
= array('date_start' => '27-10-2010', 'date_end' => '29-10-2010');

$haystack = array();
$haystack[] = array('date_start' => '25-10-2010', 'date_end' => '26-10-2010', 'promos' => array('test', 'test1');
$haystack[] = array('date_start' => '27-10-2010', 'date_end' => '28-10-2010', 'promos' => array('test2', 'test3');
$haystack[] = array('date_start' => '27-10-2010', 'date_end' => '29-10-2010', 'promos' => array('test4', 'test5');

$key = my_array_search($needle, $haystack);
// will output (bool)false for not found or (int)key_no with the key number
// this example outputs (int)2
var_dump($key);
?>

I hope this function will help someone!

GuardianGI (23-Sep-2010 08:42)

I needed to check an array for a value but the value only had to match part of a string in the array value so i wrote this little function hope it helps someone out (also some documentation of what used to test if there already was such a function 'couse i was hoping array_search was this.

<?php
function my_array_search($needle = null, $haystack_array = null, $skip = 0)
{
    if(
$needle == null || $haystack_array == null)
        die(
'$needle and $haystack_array are mandatory for functie my_array_search()');
    foreach(
$haystack_array as $key => $eval)
    {
        if(
$skip != 0)$eval = substr($eval, $skip);
        if(
stristr($eval, $needle) !== false) return true;
    }
    return
false;
}

$arr = array('foo', 'bar');

echo
'using in_array<br />';
if(
in_array('ar', $arr) === true)echo 'ar found<br />';
if(
in_array('bar', $arr) === true)echo 'bar found<br />';

echo
'using array_search<br />';
if(
array_search('ar', $arr) !== false)echo 'ar found<br />';
if(
array_search('bar', $arr) !== false)echo 'bar found<br />';

echo
'using my_array_search<br />';
if(
my_array_search('ar', $arr) !== false)echo 'ar found<br />';
if(
my_array_search('bar', $arr) !== false)echo 'bar found<br />';
?>

returns:

using in_array
bar found
using array_search
bar found
using my_array_search
ar found
bar found

j dot vd dot merwe at enovision dot net (22-Sep-2010 10:42)

The getParentStack is also working if you work with JSON records that are encoded. Following sample:

$in = '{"total":"2","records": [{"id":"25"},{"id":"32"}]}'; // string!!!
$json = json_decode( $in, true ); //don't forget the true here

$s = getParentStack(32, $json['records']) ;

then test this with:

if ($s != false)     // then it is found, don't test true here!!!

or test direct with:

if ( getParentStack(32, $json['records']) != false )

It works great with my JSON's thanks Robert Gonzalez

robert at robert-gonzalez dot com (22-Sep-2010 08:15)

I needed a way to find the parent hierarchy of a multidimensional array. Being the rogue that I am, I got to coding before searching the manual and came up with two little functions that will return a parent stack for a first find and a complete parent stack, similar in nature to the solution presented by jette at nerdgirl dot dk without all the extra stuff or use of eval(). ;)

<?php
/**
 * Gets the parent stack of a string array element if it is found within the
 * parent array
 *
 * This will not search objects within an array, though I suspect you could
 * tweak it easily enough to do that
 *
 * @param string $child The string array element to search for
 * @param array $stack The stack to search within for the child
 * @return array An array containing the parent stack for the child if found,
 *               false otherwise
 */
function getParentStack($child, $stack) {
    foreach (
$stack as $k => $v) {
        if (
is_array($v)) {
           
// If the current element of the array is an array, recurse it and capture the return
           
$return = getParentStack($child, $v);
           
           
// If the return is an array, stack it and return it
           
if (is_array($return)) {
                return array(
$k => $return);
            }
        } else {
           
// Since we are not on an array, compare directly
           
if ($v == $child) {
               
// And if we match, stack it and return it
               
return array($k => $child);
            }
        }
    }
   
   
// Return false since there was nothing found
   
return false;
}

/**
 * Gets the complete parent stack of a string array element if it is found
 * within the parent array
 *
 * This will not search objects within an array, though I suspect you could
 * tweak it easily enough to do that
 *
 * @param string $child The string array element to search for
 * @param array $stack The stack to search within for the child
 * @return array An array containing the parent stack for the child if found,
 *               false otherwise
 */
function getParentStackComplete($child, $stack) {
   
$return = array();
    foreach (
$stack as $k => $v) {
        if (
is_array($v)) {
           
// If the current element of the array is an array, recurse it
            // and capture the return stack
           
$stack = getParentStackComplete($child, $v);
           
           
// If the return stack is an array, add it to the return
           
if (is_array($stack) && !empty($stack)) {
               
$return[$k] = $stack;
            }
        } else {
           
// Since we are not on an array, compare directly
           
if ($v == $child) {
               
// And if we match, stack it and return it
               
$return[$k] = $child;
            }
        }
    }
   
   
// Return the stack
   
return empty($return) ? false: $return;
}

// TESTING
$array = array(
   
'balloon' => array(
       
'red' => array(1 => 'Love', 'Valentine', 'Heart',),
       
'green' => array(1 => 'Summertime', 'Hope',),
    ),
   
'ribbon' => array(
       
'yellow' => array(2 => 'Welcome',),
       
'red' => array(3 => 'Love', 'Love',),
    ),
);

$s = getParentStack('Love', $array);
$c = getParentStackComplete('Love', $array);
var_dump($s, $c);
?>

Output:

array
  'balloon' =>
    array
      'red' =>
        array
          1 => string 'Love' (length=4)

array
  'balloon' =>
    array
      'red' =>
        array
          1 => string 'Love' (length=4)
  'ribbon' =>
    array
      'red' =>
        array
          3 => string 'Love' (length=4)
          4 => string 'Love' (length=4)

hanan dot ali dot shaikh at googlemail dot com (17-Sep-2010 09:14)

In this code I write a code to find next and previous element of an array using current element of that array. Let suppose if we are in element 9 and have to access its next and previous element then this code be helpful for someone.

<?php
$myArray
= array(4,5,7,9,10,11,13,19,25);

$currentElement = 9;
$firstElement = current($myArray);
$lastElement = $myArray[sizeof($myArray)-1];

$currentKey = array_search($currentElement, $myArray);
$currentValue = $myArray[$currentKey];

$previousValue = "";
$nextValue = "";
if(
$currentElement!=$lastElement){
   
$nextKey = $currentKey + 1;
   
$nextValue = $myArray[$nextKey];
}

if(
$currentElement!=$firstElement){
   
$previousKey = $currentKey - 1;
   
$previousValue = $myArray[$previousKey];
}

echo
$previousValue."--".$currentValue."--".$nextValue;

?>

Thanks,
Hanan Ali

Yco (03-Aug-2010 09:35)

This a simple and lazy lazy method to search a key name in an array by pattern, and return the value if it isn't empty.

<?php
function array_search_key($p, $a)
{
    foreach (
$a as $k => $v) {
        if(
strstr($k, $p)){
            if(!empty(
$v) || $v > 0){
                return
$v;
            }
        }
    }
}
?>

giulio dot provasi at gmail dot com (30-Apr-2010 10:06)

If you want a very simple way of searching a value through a multidimensionnal array here's a  trick :

<?php
function recursiveArraySearch($haystack, $needle, $index = null)
{
   
$aIt     = new RecursiveArrayIterator($haystack);
   
$it    = new RecursiveIteratorIterator($aIt);
   
    while(
$it->valid())
    {       
        if (((isset(
$index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) {
            return
$aIt->key();
        }
       
       
$it->next();
    }
   
    return
false;
}
?>

So let's take a misc multi-dimensionnal array :

<?php
$std
= new stdClass();
$std->name     = 'luke';
$std->age     = '25';
$std->sex     = 'M';

$array     = array(    array('type'=>'dog', 'name'=>'butch', 'sex'=>'m', 'breed'=>'boxer'),
                    array(
'type'=>'dog', 'name'=>'fido', 'sex'=>'m', 'breed'=>'doberman'),
                   
'simpleValue',
                    array(
'type'=>'cat', 'name'=>'tiddles','sex'=>'m', 'breed'=>'maine coon'),
                    array(
'type'=>'horse', 'name'=>'ed','sex'=>'m', 'breed'=>'clydesdale'),
                   
$std);

echo
recursiveArraySearch($array, '25', 'age');        // returns 5
echo recursiveArraySearch($array, '25', 'name');    // returns false
echo recursiveArraySearch($array, 'simpleValue');    // returns 2
echo recursiveArraySearch($array, 'fido');            // returns 1
?>

kim at steinhaug dot com (19-Mar-2010 08:44)

I was looking around for a recursive search by keys in multidimensional arrays. After testing the ones in this thread seems noone accually works. So I put this one together, and it does what you expect it to do.

You need to find the value for the key "han-solo".

<?php
if( ($val = array_search_key('han-solo',$array)) !== false){
 
var_dump($val);
} else {
 
// No keys with the name "han-solo"
}

function
array_search_key( $needle_key, $array ) {
  foreach(
$array AS $key=>$value){
    if(
$key == $needle_key) return $value;
    if(
is_array($value)){
      if( (
$result = array_search_key($needle_key,$value)) !== false)
        return
$result;
    }
  }
  return
false;
}
?>

camden dot michael at gmail dot com (03-Mar-2010 06:58)

I needed a case insensitive array search function for a project...

<?php
/**
 * Performs the same function as array_search except that it is case
 * insensitive
 * @param mixed $needle
 * @param array $haystack
 * @return mixed
 */

function array_nsearch($needle, array $haystack) {
  
$it = new IteratorIterator(new ArrayIterator($haystack));
   foreach(
$it as $key => $val) {
       if(
strcasecmp($val,$needle) === 0) {
           return
$key;
       }
   }
   return
false;
}
?>

jette at nerdgirl dot dk (01-Mar-2010 04:07)

I use this function to search the value of arrays of any dimension, and return the result with keys preserved:

<?php
function multiArrayValueSearch($haystack, $needle, &$result, &$aryPath=NULL, $currentKey='') {
  if (
is_array($haystack)) {
   
$count = count($haystack);
   
$iterator = 0;
    foreach(
$haystack as $location => $straw) {
     
$iterator++;
     
$next = ($iterator == $count)?false:true;
      if (
is_array($straw)) $aryPath[$location] = $location;
       
multiArrayValueSearch($straw,$needle,$result,$aryPath,$location);
        if (!
$next) {
          unset(
$aryPath[$currentKey]);
        }
      }
    } else {
     
$straw = $haystack;
      if (
$straw == $needle) {
        if (!isset(
$aryPath)) {
         
$strPath = "\$result[$currentKey] = \$needle;";
        } else {
         
$strPath = "\$result['".join("']['",$aryPath)."'][$currentKey] = \$needle;";
        }
        eval(
$strPath);
      }
   }
}
?>

Example:

<?php
$ary
['ballon']['red'][1] = 'Love';
$ary['ballon']['red'][2] = 'Valentine';
$ary['ballon']['red'][3] = 'Heart';
$ary['ballon']['green'][1] = 'Summertime';
$ary['ballon']['green'][2] = 'Hope';
$ary['ribbon']['yellow'][2] = 'Welcome';
$ary['ribbon']['red'][3] = 'Love';
$ary['ribbon']['red'][4] = 'Love';

echo
"<pre>";

//Just call the function with 3 first parameters set:
//  1) The array to search
//  2) The value to find
//  3) A variable to store the result
multiArrayValueSearch($ary,'Love',$match);

print_r($match);
echo
"</pre>";
?>

Output:

Array
(
    [ballon] => Array
        (
            [red] => Array
                (
                    [1] => Love
                )

        )

    [ribbon] => Array
        (
            [red] => Array
                (
                    [3] => Love
                    [4] => Love
                )

        )

)

You can easily adapt it to suit your need of matching, by changing line 16 in the function:

<?php
if ($straw == $needle) {
?>

james at bandit.co.nz (29-Jan-2010 01:33)

A couple of staple custom PHP array searching functions I rely on in most of my projects;

<?php
   
// array_search with partial matches and optional search by key
   
function array_find($needle, $haystack, $search_keys = false) {
        if(!
is_array($haystack)) return false;
        foreach(
$haystack as $key=>$value) {
           
$what = ($search_keys) ? $key : $value;
            if(
strpos($what, $needle)!==false) return $key;
        }
        return
false;
    }
   
   
// array_search with recursive searching, optional partial matches and optional search by key
   
function array_find_r($needle, $haystack, $partial_matches = false, $search_keys = false) {
        if(!
is_array($haystack)) return false;
        foreach(
$haystack as $key=>$value) {
           
$what = ($search_keys) ? $key : $value;
            if(
$needle===$what) return $key;
            else if(
$partial_matches && @strpos($what, $needle)!==false) return $key;
            else if(
is_array($value) && array_find_r($needle, $value, $partial_matches, $search_keys)!==false) return $key;
        }
        return
false;
    }
?>

Hope they help someone!

mrjay42 at gmail dot com (13-Nov-2009 02:09)

If you search a key position in an associative array (keys are uniques), i suggest this function :
<?php
public function getKeyPositionInArray($haystack, $keyNeedle)
{
   
$i = 0;
    foreach(
$haystack as $key => $value)
    {
        if(
$key == $keyNeedle)
        {
            return
$i;
        }
       
$i++;
    }
}
?>

codeslinger at compsalot dot com (02-Oct-2009 08:22)

one thing to be very aware of is that array_search() will fail if the needle is a string and the array itself contains values that are mixture of numbers and strings.  (or even a string that looks like a number)

The problem is that unless you specify "strict" the match is done using ==    and in that case any string will match a numeric value of zero which is not what you want.

-----

also, php can lookup an index pretty darn fast.  for many scenarios, it is practical to maintain multiple arrays, one in which the index of the array is the search key and the normal array that contains the data.

<?php

  $normal
[$index] = array('key'=>$key, 'data'=>'foo');
 
$inverse[$key] = $index;

 
//very fast lookup, this beats any other kind of search

 
if (array_key_exists($key, $inverse))
  {
   
$index = $inverse[$key];
    return
$normal[$index];
  }

?>

pi3ch at gmx dot com (29-Sep-2009 05:29)

The return value for array_search is confusing if the return key is zero. it is better to double check it with in_array() function.

Example:
<?php

$SampleArray
= ('a', 'b', 'c');

$Key = array_search('a', $SampleArray);
$Zero = in_array('a', $SampleArray);

if(
$Key == NULL && !$Zero)
   echo
"Key doesnt exists";
else
   echo
"Key exists";

 
?>

pvenakis at efrontlearning dot net (24-Sep-2009 01:02)

if $haystack is not an array, for example false from some previous action, the function returns null instead of false in php 5.3.

temporal dot pl at gmail dot com (04-Sep-2009 11:57)

Sometimes you need to find a given value in a sorted array or - if not found - detect the place where it should be. After that you can for example split the array into two halves, the  greater and the smaller one.

greenmr, dennis.decoene and php at celerondude had all posted very good binary search functions but these functions all return false if the needle was not found in the haystack. I've tweaked greenmr's code a little:

<?php
function Array_BinarySearch( $needle, $haystack, $comparator , &$probe )
{
   
$high = Count( $haystack ) -1;
   
$low = 0;
   
    while (
$high >= $low )
    {
       
$probe = Floor( ( $high + $low ) / 2 );
       
$comparison = $comparator( $haystack[$probe], $needle );
        if (
$comparison < 0 )
        {
           
$low = $probe +1;
        }
        elseif (
$comparison > 0 )
        {
           
$high = $probe -1;
        }
        else
        {
            return
true;
        }
    }
   
//The loop ended without a match
    //Compensate for needle greater than highest haystack element
   
if($comparator($haystack[count($haystack)-1], $needle) < 0)
    {
       
$probe = count($haystack);
    }
    return
false;
}
?>

Now, the function returns true if it finds something and false otherwise. If a needle was found, then $probe will contain it's position. Otherwise, $probe will contain position of where the needle would be if it were there :). This is possible because we pass $probe by reference.

Example:

<?php
//ultra-simple comparator :)
function CompareNumbers($obj, $needle)
{
    return
$obj - $needle;
}

//use examples
$testArr = array(10, 20, 30, 40, 50);
$res = Array_BinarySearch(30, $testArr, 'CompareNumbers', $probe);
echo (int)
$res.' '.$probe.'<br />';
//output is: 1 2 - found at position 2

$res = Array_BinarySearch(45, $testArr, 'CompareNumbers', $probe);
echo (int)
$res.' '.$probe.'<br />';
//output is: 0 4 - not found, but it would be at position 4 (between 40 and 45)
 
$res = Array_BinarySearch(-3, $testArr, 'CompareNumbers', $probe);
echo (int)
$res.' '.$probe.'<br />';
//output is: 0 0 - not found, but it would be at position 0 (before 10)

$res = Array_BinarySearch(300, $testArr, 'CompareNumbers', $probe);
echo (int)
$res.' '.$probe.'<br />';
//output is: 0 5 - not found, but it would be at position 5 (after 50; note, that count($haystack) == 5)
?>

See original greenmr's note for additional details about usage of this binary search: http://php.net/manual/en/function.array-search.php#89413

Nguyen KimKha (23-Aug-2009 06:18)

You can remove some values from array, by using unset() and array_search().

<?php

$friends
= array( 'Bob', 'Ann', 'Peter' ); // Two persons named 'Bob'
$find = 'Bob';
$key = array_search( $find, $friends ); // Find key of given value
if ($key != NULL || $key !== FALSE) {
    unset(
$friends[$key]); // remove key from array
}

// Now, $friends = array( 'Ann', 'Peter');

?>

Frank A Cefalu frankcefalu at gmail dot com (18-Aug-2009 07:22)

The original function for searching a multidimensional array didn't let you filter by arraykeys.

This one is modified so you can search by array keys or not.

If you want a general search for a value and don't care which specific key to target you dont have to specify it but example.

<?php
$array
= ("shoes" => array("test1"=> "123","test2"=>"1234","test3"=>"12345"),
            
"shoes2"=> array("test1"=>"324","test2"=>"3515","test3"=>"123131");

array_search_value("123",$array,"test1"); // would return shoes
array_search_value("12223",$array,"test1"); // would return false;
?>
this makes it easier to validate a multi-dimensional array to make sure certain data is present.

Enjoy!

<?php

function array_search_value($needle,$haystack,$arraykey=FALSE) {
    foreach(
$haystack as $key=>$value) {
       
$current_key=$key;

        if(
$arraykey){
           
            if(
$needle == $value[$arraykey]){
            return
$value['id'];
            }
           
            if(
array_search_value($needle,$value[$arraykey]) == true) {
            return
$current_key;
            }
           
        }else{
           
            if(
$needle == $value)
            return
$value;
           
            if(
array_search_value($needle,$value) == true) {
                return
$current_key;
            }
           
           
        }
       
       
    }
    return
false;
}
?>

mikeytown2 (23-Jul-2009 12:14)

Made a function that I used to search the $_SERVER variable ( http://php.net/reserved.variables.server ) for matches to getcwd ( http://php.net/getcwd ) excluding some key's that I don't want.

<?php
/**
 * Returns all key/values in array that match.
 *
 * @param $needle
 *  What your searching for
 * @param $haystack
 *  Array of values
 * @param $a_not
 *  Optional array of key names to exclude
 */
function boost_array_find($needle, $haystack, $a_not = array()) {
 
$out = array();
  foreach(
$haystack as $key=>$value) {
    if (
strpos($value, $needle) !== FALSE) {
     
$good = TRUE;
      foreach(
$a_not as $not) {
        if (
strpos($key, $not) !== FALSE) {
         
$good = FALSE;
        }
      }
      if (
$good) {
       
$out[$key] = $value;
      }
    }
  }
  return
$out;
}

// Example Usage
$document_root = str_replace("\\", '/', getcwd());
$rejects = array('SCRIPT_FILENAME');
$out = boost_array_find($document_root, $_SERVER, $rejects);
print_r($out);
?>

Which should output this on most servers.
Array
(
    [DOCUMENT_ROOT] => path/to/webroot
)

On Mosso's servers it will output this
Array
(
    [PHP_DOCUMENT_ROOT] => path/to/webroot
)

Anonymous (22-Jun-2009 10:51)

Here is a cool trick:

<?php
function arr_search ( $array, $expression ) {
   
$result = array();
   
$expression = preg_replace ( "/([^\s]+?)(=|<|>|!)/", "\$a['$1']$2", $expression );
    foreach (
$array as $a ) if ( eval ( "return $expression;" ) ) $result[] = $a;
    return
$result;
}

$data = array (
    array (
"name" => "bill", "age" => 40 ),
    array (
"name" => "john", "age" => 30 ),
    array (
"name" => "jack", "age" => 50 ),
    array (
"name" => "john", "age" => 25 )
);

print_r ( arr_search ( $data, "age>=30" ) );
print_r ( arr_search ( $data, "name=='john'" ) );
print_r ( arr_search ( $data, "age>25 and name=='john'" ) );
?>

-- results --

Array
(
    [0] => Array
        (
            [name] => bill
            [age] => 40
        )

    [1] => Array
        (
            [name] => john
            [age] => 30
        )

    [2] => Array
        (
            [name] => jack
            [age] => 50
        )

)
Array
(
    [0] => Array
        (
            [name] => john
            [age] => 30
        )

    [1] => Array
        (
            [name] => john
            [age] => 25
        )

)
Array
(
    [0] => Array
        (
            [name] => john
            [age] => 30
        )

)

buddel (08-Jun-2009 04:13)

the recursive function by tony have a small bug. it failes when a key is 0

here is the corrected version of this helpful function:

<?php
function recursive_array_search($needle,$haystack) {
    foreach(
$haystack as $key=>$value) {
       
$current_key=$key;
        if(
$needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
            return
$current_key;
        }
    }
    return
false;
}
?>

tony dot peter at wanadoo dot fr (11-May-2009 08:53)

A simple recursive array_search function :

<?php
function recursive_array_search($needle,$haystack) {
    foreach(
$haystack as $key=>$value) {
       
$current_key=$key;
        if(
$needle===$value OR (is_array($value) && recursive_array_search($needle,$value))) {
            return
$current_key;
        }
    }
    return
false;
}
?>

n-regen (05-May-2009 06:36)

If you only know a part of a value in an array and want to know the complete value, you can use the following function:
<?php
function array_find($needle, $haystack)
{
   foreach (
$haystack as $item)
   {
      if (
strpos($item, $needle) !== FALSE)
      {
         return
$item;
         break;
      }
   }
}
?>
The function returns the complete first value of $haystack that contains $needle.

alex at softservice dot org (29-Apr-2009 05:07)

Can be useful for searching in a multi-dimensional array:

<?php
function array_search_in_level($needle, $haystack, $key, &$result, $searchlevel = 0) { 
  while(
is_array($haystack) && isset($haystack[key($haystack)])) {
    if(
$searchlevel == 0 && key($haystack) == $key && $haystack[$key] == $needle) {
     
$result = $haystack;
    } elseif(
$searchlevel > 0) {
     
array_search_in_level($needle, $haystack[key($haystack)], $key, $result, $searchlevel - 1);
    }
   
next($haystack);
  }
}
?>

Examples:

1. 2-dimensional array, search by both key and value
$arr1 = array(
  1 => array('id' => 1, 'name' => 'Alex', 'gender' => 'male'),
  ...
  12 => array('id' => 12, 'name' => 'John', 'gender' => 'male'),
);


<?php array_search_in_level('John', $arr1, 'name', $result, 1); ?>

$result is:
array(
  'id' => int 12
  'name' => string 'John' (length=4)
  'gender' => string 'male' (length=4)
)

2. 3-dimensional array, search by value only:

<?php
$arr2
= array(
 
1 => array('id' => 1, 'name' => 'Alex', 'gender' => 'male'),
 
/* ... */
 
12 => array('id' => 12, 'name' => 'John', 'gender' => 'male', 'friends' => array('Helen', 'Julia')),
);

array_search_in_level('Helen', $arr1, 0, $result, 2);
?>

$result is:
array(
  0 => string 'Helen' (length=5)
  1 => string 'Julia' (length=5)
)

office at drsoft dot com (02-Apr-2009 12:59)

If you wish to search a multidimensional array by keys:

<?php
function extract_values_by_key ( $array, $needle_key, &$out = array () ) {
    foreach ( (array)
$array as $key => $value ) {
        if ( !
is_array ( $value ) && $key == $needle_key ) {
           
array_push ( $out, $value );
        }
        else {
           
extract_values_by_key ( $value, $needle_key, &$out );
        }
    }

    return
$out;
}
?>

elie at najberg dot fr (27-Mar-2009 02:04)

This is how to search a part of a val in an array :

<?php
     
function array_ereg_search($val, $array) {
         
         
$i = 0;
         
$return = array();
         
          foreach(
$array as $v) {
             
               if(
eregi($val, $v)) $return[] = $i;
              
$i++;
              
          }
         
      return
$return;
     
      }
?>

greenmr (06-Mar-2009 07:02)

Incidentally, the KeyCompare() function I described above is a generalized version. Since array_bsearch() doesn't care about specific return values, just greater then, less than, or equal to zero, when you are comparing two numbers you can greatly simplify the comparison function to something like:

<?php
 
function KeyCompare( $obj, $needle ) {
      return
$obj['my_key'] - $needle;
  }
?>

greenmr (06-Mar-2009 06:15)

As others have noted, if your array is already sorted, a binary search is the way to go. When I realized PHP doesn't have a native one, I came here to see what others had done to implement this workhorse algorithm. While I appreciate those that have posted their own solutions here, none of those implementations met my needs. Also, due mainly to non-optimal loop processing, the code offered required extra post-processing to handle various ways the search loop may have exited. Proper coding of initial conditions and the main loop eliminates this extra processing.

Also, the solutions presented assumed a simple one-dimensional array and basic comparisons. I needed to search an array of user objects, with the comparison performed on one of the object attributes. C/C++ handles the need for comparison flexibility by the binary search function taking a parameter that points to a user function that performs any arbitrarily complex comparison and returns a defined result depending on the relative magnitudes of the compared entities. Since PHP allows similar function reference parameters, I realised it would be quite easy to implement this functionality.

I won't go into an explanation of the algorithm chosen for the main binary search function, google "binary searches" if you don't understand how they work. I will mention however that the ONLY way to exit the main loop in this function is on a match, or a definite fail. The loop will never terminate while the result of the search is indeterminate.

Some solutions I've seen here and elsewhere return the index of the found element on a match, and "false" on a fail. Since PHP treats a zero the same as false in expressions, and zero is a valid array index, using false to designate "not found" gives an ambiguous result in some cases. I chose to use the value -1 to signify a search failure instead.

<?php
   
function array_bsearch( $needle, $haystack, $comparator ) {
       
$high = Count( $haystack ) -1;
       
$low = 0;
       
        while (
$high >= $low ){
           
$probe = Floor( ( $high + $low ) / 2 );
           
$comparison = $comparator( $haystack[$probe], $needle );
            if (
$comparison < 0 ) {
               
$low = $probe +1;
            } elseif (
$comparison > 0 ) {
               
$high = $probe -1;
            } else {
                return
$probe;
            }
        }
     
     
// ---The loop ended without a match
     
return -1;
    }
?>

In this function the parameters "$needle" and "$haystack" have the same meaning as for the native array_search() PHP function. The third parameter, "$comparator" is the name of the function to use to determine if an array element matches the needle.

The array_bsearch() function is self-contained, and doesn't need modification no matter how complex the comparison needs to be. The actual comparison is performed by a purpose-written user function that takes two parameters and returns a numerical result. array_bsearch() will call this function each time it needs to compare an array element to the needle. The first parameter passed will be the element to test, and the second will the the needle value passed to array_bsearch(). Note that if you will always search for the same thing your user function can ignore the second parameter and hard-code the test on the first parameter.

The comparison user function must return a positive number if the element is greater than the needle, a negative number if it is smaller than the needle, and zero if they match.

Given a scenario where you want to find the index of a user object loaded from MySQL in an array, where the "my_key" property equals 54. Here's how you could code it using a user function and the new array_bsearch() function.

<?php
 
function KeyCompare( $obj, $needle ) {
      if (
$obj['my_key'] < $needle ) {
          return -
1;
      } elseif (
$obj['my_key'] > $needle ) {
          return
1;
      } else {
          return
0;
      }
  }

 
$index = array_bsearch( 54, $my_array, 'KeyCompare' );
?>

Hope this is helpful to somebody out there.

info at [hidden] dot apanel dot nl (04-Feb-2009 01:27)

I made this function after I didnt find a recursive array_search function with a limit and the possibility to inverse the search. So here is it:

<?php

function search_in_array ($needle, $haystack, $inverse = false, $limit = 1) {
   
   
# Settings
   
$path = array ();
   
$count = 0;
   
   
# Check if inverse
   
if ($inverse == true)
       
$haystack = array_reverse ($haystack, true);
       
   
# Loop
   
foreach ($haystack as $key => $value) {

       
# Check for return
       
if ($count > 0 && $count == $limit)   
            return
$path;
       
       
# Check for val
       
if ($value === $needle) {
           
           
# Add to path
           
$path[] = $key;
           
           
# Count
           
$count++;
           
        } else if (
is_array ($value)) {
           
           
# Fetch subs
           
$sub = search_in_array ($needle, $value, $inverse, $limit);
           
           
# Check if there are subs
           
if (count ($sub) > 0) {
               
               
# Add to path
               
$path[$key] = $sub;
               
               
# Add to count
               
$count += count ($sub);
            }
        }
    }
   
    return
$path;
}

?>

helenadeus at gmail dot com (25-Jan-2009 09:05)

I was trying to use array_search to retrieve all the values that match a given needle, but it turns out only the first match key is returned. I built this little function, which works just like array_search, but returns all the keys that match a given needle instead. The output is an array.

<?php

$haystack
= array('a','b','a','b');

$needle = 'a';

print_r(array_search_all($needle, $haystack));

//Output will be
// Array
// (
//         [0]=>1
//         [1]=>3
// )

function array_search_all($needle, $haystack)
{
#array_search_match($needle, $haystack) returns all the keys of the values that match $needle in $haystack

   
foreach ($haystack as $k=>$v) {
   
        if(
$haystack[$k]==$needle){
       
          
$array[] = $k;
        }
    }
    return (
$array);

   
}

?>

tim_hauber at stev dot net (07-Jan-2009 04:50)

A slight addition to array_search_recursive by Alireza Eliaderani below.  The original searched the entire haystack, even if it found the needle at the beginning of the haystack. I must admit I had a little trouble wrapping my head around how to break out of a loop in a recursive function.  This version returns after first occurrence of needle. changes commented.

<?php
function array_search_recursive($needle, $haystack){
   
$path=array();
    foreach(
$haystack as $id => $val)
    {
 
         if(
$val === $needle) {
             
$path[]=$id;
 
             break;
            
# ^^this breaks out of loop when it finds needle

        
} else if(is_array($val)){
            
$found=array_search_recursive($needle, $val);
              if(
count($found)>0){
                 
$path[$id]=$found;
 
                 break;
               
# ^^this breaks out of loop when recursive call found needle
 
            
}     
          }
      }
      return
$path;
}
?>

azaozz, gmail (21-Dec-2008 01:23)

Expanding on the comment by hansen{}cointel.de:

When searching for a string and the array contains 0 (zero), the string is casted to (int) by the type-casting which is always 0 (perhaps the opposite is the proper behaviour, the array value 0 should have been casted to string). That produces unexpected results if strict comparison is not used:

<?php
$a
= array(0, "str1", "str2", "str3");
echo
"
str1 = "
.array_search("str1", $a).",
str2 = "
.array_search("str2", $a).",
str3 = "
.array_search("str3", $a).",

str1 strict = "
.array_search("str1", $a, true).",
str2 strict = "
.array_search("str2", $a, true).",
str3 strict = "
.array_search("str3", $a, true);
?>

This will return:
str1 = 0, str2 = 0, str3 = 0, str1 strict = 1, str2 strict = 2, str3 strict = 3

grezvany13 [at] notitia.nl (11-Dec-2008 11:32)

This function can search for an array or string within an array.

<?php
function array_search_array($needle, $haystack){
    if(
is_array($needle) && is_array($haystack)) {
       
$tmp = array_diff_assoc($needle, $haystack);
        if(empty(
$tmp)) return true;
        foreach(
$haystack as $value) {
            if(
is_array($value)) {
               
$tmp = array_diff_assoc($needle, $value);
                if(empty(
$tmp)) return true;
            }
        }
    }
    if(
is_array($haystack) && !is_array($needle)) {
        foreach(
$haystack as $value) {
            if(
is_array($value)) {
               
$tmp = array_diff_assoc($needle, $value);
                if(empty(
$tmp)) return true;
            } elseif(
$needle == $value) return true;
        }
    } elseif(
$needle == $haystack) return true;
    return
false;
}
?>

It's not recursive yet, so please improve it if you feel for it ;)

Alireza Eliaderani (25-Nov-2008 06:42)

this function return a multidimensional path of recursively founded $needle in a multidimensional $haystack

<?php
function array_search_recursive($needle, $haystack){
   
$path=array();
    foreach(
$haystack as $id => $val)
    {
 
         if(
$val === $needle)
             
$path[]=$id;
         else if(
is_array($val)){
            
$found=array_search_recursive($needle, $val);
              if(
count($found)>0){
                 
$path[$id]=$found;
              }      
          }
      }
      return
$path;
}
?>

michi7x7 (17-Nov-2008 07:32)

If you exit the function whith "return", there is no need to break ;)

i would write it like this:

<?php

function array_search_recursive($needle, $haystack, $path=array())
{
    foreach(
$haystack as $id => $val)
    {
        
$path2=$path;
        
$path2[] = $id;
 
         if(
$val === $needle)
              return
$path2;
         else if(
is_array($val))
              if(
$ret = array_search_recursive($needle, $val, $path2))
                   return
$ret;
      }
      return
false;
}
 
$array = array(
  
"a" => array(
      
"b" => array("c"),
      
"d" => array("e"),
       ),
   
"f" => array( "g", "h"),
    );
 
var_dump(array_search_recursive("e", $array));

?>

var dump prints a array: (a, d, 0)

s0i0m at dreamevilconcepts dot com (12-Sep-2008 10:39)

I wrote a better array_search_recursive function.  The other functions listed work and will evaluate to true, however, they loop through the stack even after the needle is found: so this function will take the overhead off the cpu =P

<?php

 
function array_search_recursive($needle, $haystack)
  {    
    foreach (
$haystack as $k => $v)
    {
      for (
$i=0; $i<count($v); $i++)
        if (
$v[$i] === $needle)
        {
          return
true;
          break;
        }
    }
  }

 
$access['admin'] = array('nick1');
 
$access['voice'] = array('nick2', 'nick3');

  if (
array_search_recursive('nick3', $access))
    echo
'yes';
 
print_r($access);

?>

I wrote this for an access list that I have implemented into an IRC bot.  See more interesting stuff at dreamevilconcept's forum.

dcez at land dot ru (30-Apr-2008 09:15)

Simple way to get variable name by using array_search function:

<?php

function varname($var){

    return (isset(
$var))? array_search($var, $GLOBALS) : false;

}

$boogie = 'tonight';

echo
varname($boogie);

?>

andreas dot damm at maxmachine dot de (04-Apr-2008 12:07)

Combining syntax of array_search() and functionality of array_keys() to get all key=>value associations of an array with the given search-value:
<?php
function array_search_values( $m_needle, $a_haystack, $b_strict = false){
    return
array_intersect_key( $a_haystack, array_flip( array_keys( $a_haystack, $m_needle, $b_strict)));
}
?>

Usage:
<?php
$array1
= array( 'pre'=>'2', 1, 2, 3, '1', '2', '3', 'post'=>2);
print_r( array_search_values( '2', $array1));
print_r( array_search_values( '2', $array1, true));
print_r( array_search_values( 2, $array1, true));
?>

Will return:
array(4) {
    ["pre"] =>
    string(1) "2"
    [1] =>
    int(2)
    [4] =>
    string(1) "2"
    ["post"] =>
    int(2)
}
array(2) {
    ["pre"] =>
    string(1) "2"
    [4] =>
    string(1) "2"
}
array(2) {
    [1] =>
    int(2)
    ["post"] =>
    int(2)
}

dot dot dot dot dot alexander at gmail dot com (09-Mar-2008 04:30)

This is the phpfied version of the array_search function for PHP version under 4.0.5

<?php
if(!function_exists("array_search")){
    function
array_search( $needle, $haystack, $strict = FALSE ){
        if( !
is_array($haystack) )return FALSE;
        foreach(
$haystack as $key => $val){
            if(   (  (
$strict ) && ( $needle === $val )  ) || (  ( !$strict ) && ( $needle == $val )  )   )return $key;
        }
        return
FALSE;
    }
/* endfunction array_search */
}/* endfunction exists array_search*/
?>

mjaning at gmail dot com (06-Feb-2008 07:42)

Hi! Based on Chris function, I made another to simplify code and improve diferents features...

With this function you can:
- Filter Key and Values recursively
- Call function many times using previows result
- Final result always will be an array numeric index contenting a value or sigle array key=>value.

Thanks for PHP!

<?php
function array_search_recursive($needle, $haystack, $nodes=array())
{     
  foreach (
$haystack as $key1=>$value1)
  {
    if (
is_array($value1))
     
$nodes = array_search_recursive($needle, $value1, $nodes);
   elseif ((
$key1 == $needle) or ($value1 == $needle))
     
$nodes[] = array($key1=>$value1);
  }
  return
$nodes;
}
   
$arg[] = array("column1"=>"Class3");
$arg[] = array("column2"=>"Class1");
$arg[] = array("column3"=>"Class3");
$arg[] = array("column4"=>"Class4");
$arg[] = array("column4"=>"Class3");
$arg[] = "column3";
   
$filter = array_search_recursive("Class3",$arg);
echo
"<hr>";var_dump($filter);
   
$filter = array_search_recursive("column3",$filter);
echo
"<hr>";var_dump($filter);
?>

pmmout at gmail dot com (29-Jan-2008 12:55)

MultiArray find function.
After some time unsuccessful looking for algorithm to find a string in multidimensional array I wrote one:

<?php
function multidimArrayLocate($array, $text){
  foreach(
$array as $key => $arrayValue){
    if (
is_array($arrayValue)){
      if (
$key == $text) $arrayResult[$key] = $arrayValue;
     
$temp[$key] = multidimArrayLocate($arrayValue, $text);
      if (
$temp[$key]) $arrayResult[$key] = $temp[$key];
    }
    else{
      if (
$key == $text) $arrayResult[$key] = $arrayValue;
    }
  }
  return
$arrayResult;
}
?>

steev at anticulture dot co dot uk (13-Jan-2008 10:46)

Just adding my two penneth to someone else's script from below. If you need to easily index your csv use this slightly modified script to parse a csv file into an associative array 2d, indexed by the first column value

<?php
function buildStock($File) {
       
$handle = fopen($File, "r");
       
$fields = fgetcsv($handle, 1000, ",");

        while(
$data = fgetcsv($handle, 1000, ",")) {
           
$detail[] = $data;
        }
        echo
"details";
       
var_dump($detail);
        echo
"<br />";

       
$x = 0;
       
$y = 0;

        foreach(
$detail as $i) {
            foreach(
$fields as $z) {
               
//original code
                //$stock[$x][$z] = $i[$y];
               
$stock[$i['0']][$z] = $i[$y];
               
$y++;
            }
           
$y = 0;
           
$x++;
        }
        return
$stock;
    }

var_dump(buildStock("conf.csv"));
?>

toni dot garcia dot gutsens at gmail dot com (02-Dec-2007 01:35)

Make a recursive search on array and return the matches elements in the same structure than the original array and unset empty arrays:

<?php
function array_search_recursive($needle, $haystack,&$tree=Array(),$index=""){
        if (
is_array($haystack)){
            if (
count($tree)==0) $tree=array_merge(Array(),$haystack);
            foreach(
$haystack as $k=>$current){
                if (
is_array($current)){
                   
array_search_recursive($needle,$current,$tree,$index."[$k]");
                    eval(
"\$a=\$tree{$index}[{$k}];"); // unset all elements = empty array
                   
if (count($a)==0//is empty?
                       
eval("unset(\$tree{$index}[$k]);"); // unset array
               
}
                else{
                    if (
$current!=$needle){
                        eval(
"unset(\$tree{$index}[{$k}]);");
                    }
                }
            }
        }
        return
$tree;
    }
?>

chris (21-Nov-2007 03:11)

<?php

/*Another recursive array_search that works better for me
* Returns all the keys to all the needles found
*and put them in an array.
*/

function array_search_recursive($needle, $haystack, $a=0, $nodes_temp=array()){
global
$nodes_found;
 
$a++;
  foreach (
$haystack as $key1=>$value1) {
   
$nodes_temp[$a] = $key1;
    if (
is_array($value1)){    
     
array_search_recursive($needle, $value1, $a, $nodes_temp);
    }
    else if (
$value1 === $needle){
     
$nodes_found[] = $nodes_temp;
    }
  } 
  return
$nodes_found;
}
?>

kermes [at] thesevens [dot] net (11-Sep-2007 11:09)

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'));
?>

robertark at gmail dot com (02-Sep-2007 07:20)

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

php5 site builder (23-May-2007 06:14)

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.

elvenone at gmail dot com (27-Jan-2007 01:10)

<?php
               
// Search an array in reverse order.
       
function array_reverse_search($value, $array) {
             for(
$i = sizeof($array)-1; $i>=0; $i--) {
                if (
$array[$i] == $value) return $i;
             }
            return -
1;    
        }
?>

francois at tekwire dot net (18-Jan-2007 10:34)

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).

erick dot xavier at gmail dot com (04-Jan-2007 12:57)

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
*/
?>

otto at twbc dot net (14-Dec-2006 07:06)

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.

<?php
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;
}
?>

evert_18 at hotmail dot com (13-Nov-2006 02: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);
    }
}
?>

dmitry dot polushkin at gmail dot com (22-Oct-2006 06:03)

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));
?>

jupiter at nospam dot com (29-Sep-2006 10:25)

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.

Digitally Designed dot co dot uk (28-Sep-2006 10:48)

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.

<?php   

function myMulti_Array_Search($theNeedle, $theHaystack, $keyToSearch)
        {
        foreach(
$theHaystack as $theKey => $theValue)
            {
           
$intCurrentKey = $theKey;   
               
            if(
$theValue[$keyToSearch] == $theNeedle)
                {
   
                return
$intCurrentKey ;
                }
            else
                {
                return
0;
                }
            }
        }

?>

lars-magne (22-Sep-2006 01:19)

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)
*/

?>

mark dot php at mhudson dot net (12-Sep-2006 01:49)

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()

ob at babcom dot biz (28-Aug-2006 10:55)

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.

(26-Jul-2006 03:19)

<?php
/**
 * 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;
}
?>

gullevek at gullevek dot org (19-Apr-2006 06:31)

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:

<?php
    $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
                )

        )

)

RichGC (20-Mar-2006 01:54)

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
?>

congaz at yahoo dot dk (10-Mar-2006 03:38)

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...

chappy at citromail dot hu (11-Feb-2006 08:26)

If you're searching for strings and you need a case-insensetive script, there's one:

<?php
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().

hansen{}cointel.de (09-Feb-2006 02:26)

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.

ludwig_von_rocht at yahoo dot com (23-Nov-2005 11:50)

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;
    }
}
?>

mark meves (22-Nov-2005 11:13)

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)
*/
?>

pornsak at neowin dot net (21-Nov-2004 03:28)

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;
}
?>

scripts at webfire dot org (03-Nov-2004 05:13)

* 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

Flix Cloutier <felixcca at yahoo dot ca> (22-Oct-2004 01:01)

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... ;)

Wouter van Vliet <me at woutervanvliet dot nl> (19-Aug-2004 04:55)

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]

andrey at php dot net (08-Aug-2004 12:56)

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

softexpert [at] libertysurf [dot] fr (03-May-2004 07:51)

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
//)

?>

dcsoboda at oakland dot edu (28-Jan-2004 09:13)

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.

dennis dot decoene at removthis dot moveit dot be (17-Jan-2004 06:41)

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;
    }
}
?>

jwhite at ytztech dot com (19-Nov-2003 02:59)

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
"<P>String Test:&nbsp; $time_str seconds.</P>\r\n";
  echo
"<P>Array Test:&nbsp; $time_array seconds.</P>\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

php at celerondude dot com (12-Nov-2003 11:07)

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. :)

cue at openxbox dot com (10-Jun-2003 03:50)

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]);
}
?>

richard at richard-sumilang dot com (16-Apr-2003 11:59)

<?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;
    }
?>

Darkvie (16-Mar-2003 12:53)

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

retestro_REMOVE at SPAM_esperanto dot org dot il (01-Mar-2003 04:46)

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.

saltymeat at hotmail dot com (24-Jun-2002 10:03)

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

dakota at dir dot bg (29-May-2002 12:44)

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.

swbrown at ucsd dot edu (02-May-2002 09:18)

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!

saconner at iastate dot edu (04-Feb-2002 09:16)

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 )