User Contributed Notes

jphansen at uga dot edu 08-Oct-2021 07:18
Warning, array_search() and in_array() both search for values exactly matching the value of an element, and don't match if a value is within the value of an element. Example:

$a = array("test");
if (array_search("te", $a) !== false)
  echo "found";
else
  echo "not found";

// outputs "not found"

You'll need to iterate the array with a strpos() check if you'd like to search for a value within a value of an array.

foreach ($a as $val) {
                if (strpos($val, "te")  !== false) {
                                echo "found"
                }
}
// outputs "found"
opencart dot ocfilter at gmail dot com 23-Sep-2021 12:43
Be careful!

<?php

var_dump
(array_search('needle', [ 0 => 0 ])); // int(0) (!)

var_dump(array_search('needle', [ 0 => 0 ], true)); // bool(false)

?>

But, in php 8

<?php

var_dump
(array_search('needle', [ 0 => 0 ])); // bool(false)

?>
moe at moose dot at 24-Jul-2021 10:44
My function get_key_in_array() needed some improvement:

<?PHP

// search keymap $s in array $a
function get_key_in_array($s, $a)
{
  if(
is_array($s) && isset($a[key($s)]) && is_array($a[key($s)])) {
    return
get_key_in_array(reset($s), $a[key($s)]);
  } else {
    if(
is_array($s)) $s=reset($s);
    return (
is_string($s)||is_int($s))&&isset($a[$s])?$a[$s]:false;
  }
}

?>
moe at moose dot at 21-Jul-2021 10:39
I needed a function, that returns a value by specifying a keymap to the searched value in a multidimensional array and came up with this.

$s is the  keymap to the needle and $a the haystack array.

<?PHP

// search keymap $s in array $a
function get_key_in_array($s, $a)
{
  if(
is_array($s) && is_array($a[key($s)])) {
    return
get_key_in_array(reset($s), $a[key($s)]);
  } else {
    if(
is_array($s)) $s = reset($s);
    return isset(
$a[$s])?$a[$s]:false;
  }
}

// usage examples
// ------------------------------
$arr = ['foo'=>['bar'=>['baz'=>77]]];

$s = ['foo'=>['bar'=>'baz']];
var_dump(get_key_in_array($s,$arr));

$s = ['foo'=>['bar'=>['baz']]];
var_dump(get_key_in_array($s,$arr));

$s = ['foo'=>['bar'=>['nix']]];
var_dump(get_key_in_array($s,$arr));

$s = ['foo'=>'bar'];
var_dump(get_key_in_array($s,$arr));

$s = ['foo'];
var_dump(get_key_in_array($s,$arr));

$s = 'foo';
var_dump(get_key_in_array($s,$arr));

?>
vikingjs+phpnet at mac dot com 22-Jun-2021 12:05
An implementation of a search function that uses a callback, to allow searching for objects of arbitrary complexity:

<?php
function array_usearch(array $array, Closure $test) {
   
$found = false;
   
$iterator = new ArrayIterator($array);

    while (
$found === false && $iterator->valid()) {
        if (
$test($iterator->current())) {
           
$found = $iterator->key();
        }
       
$iterator->next();
    }

    return
$found;
}
?>

The above takes a closure, rather than a callable, but that could easily be altered. Like array_search, this will return false if no match is found. I made the closure the last parameter in case php one day implements trailing-closure syntax.

For instance, if you have an array of objects with an id property, you could search for the object with a specific id like this:

<?php
class Thing  {
    public
$id;
    public
$name;

    public function
__construct($id, $name, $category) {
       
$this->id = $id;
       
$this->name = $name;
    }
}

$listOfThings = [
    new
Thing(1, 'one'),
    new
Thing(2, 'two'),
    new
Thing(3, 'three'),
    new
Thing(4, 'four'),
];

$id4Index = array_usearch($listOfThings, function($thing) {
    return
$thing->id === 4;
});
?>

Since the Closure can capture information, it becomes simple to write a function that takes search criteria as parameters, creates a closure with those criteria, and calls the above. A simple example that does the same search as above would be

<?php
$idClosure
= function($id) {
    return function(
$item) use ($id) {
        return
$item->id = $id;
    }
}

$id4Index = array_usearch($idClosure(4));
?>

It's more complex to set up, but now any search for an id can be written in a single line, making things clean and concise.

For a more complex example, this function takes an array of key/value pairs and returns the key for the first item in the array that has all those properties with the same values.

<?php
function firstIndexMatching(array $array, array $criteria, bool $useStrict = true) {

    if (
count($criteria) < 1) {
        return
false;
    }

   
// create a closure that has captured the search criteria
   
$testWithCriteria = function($criteria, $useStrict) {

        return function(
$item) use ($criteria, $useStrict) {

            foreach(
$criteria as $key => $value) {
                if (!isset(
$item->$key)) {
                    return
false;
                } else if (
$useStrict && $item->$key !== $value) {
                    return
false;
                } else if (!
$useStrict && $item->$key != $value) {
                    return
false;
                }
            }

            return
true;
        };
    };

    return
array_usearch($array, $testWithCriteria($criteria, $useStrict));
}
?>

If you have a list of people, for instance, you can now simply find the person record with a specific first and last name:

<?php
$joeSchlabotnikIndex
= firstIndexMatching($people, [
   
'firstName' => 'Joe',
   
'lastName' => 'Schlabotnik'
]);
?>

Obviously firstIndexMatching could be modified to use case-insensitive comparisons or anything else that fits your use-case. They idea is capturing the input data.

The final step is a function that returns the item, rather than its key, or null if no match found:

<?php
function firstItemMatching(array $array, array $criteria, bool $useStrict = true) {
   
// fun fact: $array[false] is equivalent to $array[0]
   
$index = firstIndexMatching($array, $criteria, $useStrict);
    return
$index !== false ? $array[$index] ?? null : null;
}
?>
vikingjs+phpnet at mac dot com 01-Jun-2021 08:15
An implementation of a search function that uses a callback, to allow searching for objects of arbitrary complexity:

<?php
function array_usearch(array $array, Closure $test) {
   
$found = false;
   
$iterator = new ArrayIterator($array);

    while (
$found === false && $iterator->valid()) {
        if (
$test($iterator->current())) {
           
$found = $iterator->key();
        }
       
$iterator->next();
    }

    return
$found;
}
?>

The above takes a closure, rather than a callable, but that could easily be altered. Like array_search, this will return false if no match is found. I made the closure the last parameter in case php one day implements trailing-closure syntax.

For instance, if you have an array of objects with an id property, you could search for the object with a specific id like this:

<?php
class Thing  {
    public
$id;
    public
$name;

    public function
__construct($id, $name, $category) {
       
$this->id = $id;
       
$this->name = $name;
    }
}

$listOfThings = [
    new
Thing(1, 'one'),
    new
Thing(2, 'two'),
    new
Thing(3, 'three'),
    new
Thing(4, 'four'),
];

$id4Index = array_usearch($listOfThings, function($thing) {
    return
$thing->id === 4;
});
?>

Since the Closure can capture information, it becomes simple to write a function that takes search criteria as parameters, creates a closure with those criteria, and calls the above. A simple example that does the same search as above would be

<?php
$idClosure
= function($id) {
    return function(
$item) use ($id) {
        return
$item->id = $id;
    }
}

$id4Index = array_usearch($idClosure(4));
?>

It's more complex to set up, but now any search for an id can be written in a single line, making things clean and concise.

For a more complex example, this function takes an array of key/value pairs and returns the key for the first item in the array that has all those properties with the same values.

<?php
function firstIndexMatching(array $array, array $criteria, bool $useStrict = true) {

    if (
count($criteria) < 1) {
        return
false;
    }

   
// create a closure that has captured the search criteria
   
$testWithCriteria = function($criteria, $useStrict) {

        return function(
$item) use ($criteria, $useStrict) {

            foreach(
$criteria as $key => $value) {
                if (!isset(
$item->$key)) {
                    return
false;
                } else if (
$useStrict && $item->$key !== $value) {
                    return
false;
                } else if (!
$useStrict && $item->$key != $value) {
                    return
false;
                }
            }

            return
true;
        };
    };

    return
array_usearch($array, $testWithCriteria($criteria, $useStrict));
}
?>

If you have a list of people, for instance, you can now simply find the person record with a specific first and last name:

<?php
$joeSchlabotnikIndex
= firstIndexMatching($people, [
   
'firstName' => 'Joe',
   
'lastName' => 'Schlabotnik'
]);
?>

Obviously firstIndexMatching could be modified to use case-insensitive comparisons or anything else that fits your use-case. They idea is capturing the input data.

The final step is a function that returns the item, rather than its key, or null if no match found:

<?php
function firstItemMatching(array $array, array $criteria, bool $useStrict = true) {
   
// fun fact: $array[false] is equivalent to $array[0]
   
$index = firstIndexMatching($array, $criteria, $useStrict);
    return
$index !== false ? $array[$index] ?? null : null;
}
?>
trash-11 at mail dot ru 03-Feb-2021 12:22
array_search of null doesn't return FALSE, be careful
joe at white-fire dot co dot uk 18-May-2020 09:32
By extension of  `sunelbe at gmail dot com` solution to search for a specific array element with key value in a multidimensional array, this one also returns the parent.

<?php
function array_search_multi(
   
$array,
   
$key,
   
$value,
   
$parent = false
) {
   
$results = array();

    if (
is_array($array)) {
        if (isset(
$array[$key]) && $array[$key] == $value)
           
$results[] = $array;

        foreach (
$array as $id => $subarray) {
           
$found = array_search_multi(
               
$subarray, $key, $value
           
);

            if(!empty(
$found)) {
                if(
$parent) {
                   
$results[$id] =
                       
$array[$id];
                } else {
                   
$results = $found;
                }
            }
        }
    }

    return
$results;
}

$array = array(
       
'key1' => array(
           
'ID' => 1,
           
'key2' => array(
               
'ID' => 2,
               
'key3' => array(
                   
'ID' => 3
               
)
            )
        ),
       
'key4' => 4,
       
'key6' => array(
           
'ID' => 5,
           
'key7' => array(
               
'ID' => 6,
               
'key8' => array(
                   
'ID' => 7,
                   
'value' => 'test'
               
)
            )
        )
);

array_search_multi($array, 'ID', 7, false);

[
0] => Array
        (
            [
ID] => 7
           
[value] => test
       
)

array_search_multi($array, 'ID', 7, true);

[
key6] => Array
        (
            [
ID] => 5
           
[key7] => Array
                (
                    [
ID] => 6
                   
[key8] => Array
                        (
                            [
ID] => 7
                           
[value] => test
                       
)

                )
nordseebaer at gmx dot de 08-May-2020 08:08
It's really important to check the return value is not false! I used array_search() to determine the index of an value to unset this value and then realized that $arr[false] === $arr[0] !

<?php
$arr
= ['apple', 'banana'];

var_dump($arr[0] === 'apple'); // true
var_dump($arr[false] === $arr[0]); // true
var_dump($arr[false] === 'apple'); // true

unset($arr[array_search('banana', $arr)]); //index = 1
var_dump($arr);

// result
//   array(1) {
//     [0]=>
//     string(5) "apple"
//   }

unset($arr[array_search('peach', $arr)]); //not found, result is false
var_dump($arr);

// result
//   array(0) {
//   }
// because $arr[false] === $arr[0]
?>

So always check the return of array_search!
yasien dot dwieb at gmail dot com 28-Apr-2020 05:41
Beware when using array_search to a mix of string and integer where prefixes of keys may collide, as in my case I have encountered the following situation:

Assume you have the following array:
<?php
$arr
= [
          
1 => 'index 0',
          
2 => 'index 1',
          
3 => 'index 2',
          
'3anothersuffix' => 'index 3'
];

$index1 = array_search('3', array_keys($arr)); // 2
$index2 = array_search('3anothersuffix', array_keys($arr)); //2
?>

$index1 and $index2 will be the same

after using strict type search:

<?php
$index1
= array_search('3', array_keys($arr), true); // false
$index2 = array_search('3anothersuffix', array_keys($arr), true);  //3
?>

it will not find $index1 at all while returning a correct value for $index2;
it2 dot mnr at gmail dot com 04-Apr-2020 06:54
About searcing in multi-dimentional arrays;
note on "xfoxawy at gmail dot com":

If you use your own keys in a multidimensional array, this will not work. Better use for this situation:

array_search($searchingValue, array_map(function($val){return $val['KEY_NAME'];}, $inputArray));
David Spector 10-Aug-2019 11:16
For data lookups that are fast in both directions (key to value, value to key), consider storing and updating both the array and its array_flip() version.
David Spector 10-Aug-2019 11:15
The fastest way to search for a cached value, to find if there is a cycle in your data, to see if a given program state has occurred before, to see whether a value is in a set and then adding it, and for so many similar tasks, is just to do an array dereference:

    $Key=$SomeStateString1.'|'.$SomeStateString2; // State can have multiple substates
    if (@$StateSet[$Key]) // or use isset()
        ; // The key already exists: do optional processing here
    else
        ; // The key does not yet exist: do optional processing here
    $StateSet[$Key]=1; // Store state existence (or store associated data values)
pvv at renam dot md 17-Jan-2019 08:12
About searcing in multi-dimentional arrays; 
note on "xfoxawy at gmail dot com" and turabgarip at gmail dot com;

$xx = array_column($array, 'NAME', 'ID');
will produce an array like :
 $xx = [
[ID_val] => NAME_val
[ID_val] => NAME_val
        ]

so:

$yy = array_search('tesxt', array_column($array, 'NAME', 'ID'));
will output expected ID;
venkatesh dot ksnadar at gmail dot com 06-Nov-2018 11:49
you need to be careful if you are using array_search() with unset() for deleting a value in the searched indexed

// wrong way
$checked = ["1", "2", "3"]; // array with numbers in double quotes

unset($checked[array_search('on', $checked)]); // this deletes value at 0 index

// array_search returns mixed data type, which means any type of data will be returned
// in the above case it returned false and false is type casted to 0
// so it deletes the value at index zero

the point to say is never use array_search() with unset() while deleting a value in an array using index.
grayaahammed at gmail dot com 17-May-2018 12:47
this is for searching a value inside a multidimontionnal array, and then return the parent of the parent array that holds the value.

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 $haystack[$key];
        }
    }
    return false;
}
miguelserranocabeza at gmail dot com 18-Apr-2018 10:56
For multiarray values i got this solution ;)

$taxonomy = array(
    array('index' => array('AU200_AUD','CN50_USD')),
    array('mt' => array('XAG_GBP','XAG_HKD','XAU_GBP','XAU_HKD','XAU_JPY')),
    array('aud' => array('AUD_CAD','AUD_CHF','AUD_HKD','AUD_JPY','AUD_NZD','AUD_SGD','AUD_USD')),
    array('cad' => array('CAD_CHF','CAD_HKD','CAD_JPY','CAD_SGD','CHF_HKD','CHF_JPY','CHF_ZAR')),
    array('eur' => array('EUR_AUD','EUR_CAD','EUR_CHF','EUR_NOK','EUR_NZD','EUR_PLN','EUR_ZAR')),
    array('gbp' => array('GBP_AUD','GBP_CAD','GBP_CHF','GBP_USD','GBP_ZAR')),
    array('hkd' => array('HKD_JPY')),
    array('nzd' => array('NZD_CAD','NZD_CHF','NZD_HKD','NZD_JPY','NZD_SGD','NZD_USD')),
    array('sgd' => array('SGD_CHF','SGD_HKD','SGD_JPY')),
    array('try' => array('TRY_JPY')),
    array('usd' => array('USD_CAD','USD_CHF','USD_CNH','USD_SGD','USD_THB','USD_TRY','USD_ZAR')),
    array('zar' => array('ZAR_JPY')),
  );

  foreach ($taxonomies as $key => $tax) {
    foreach ($tax as $value) {
      if(array_search('SGD_HKD', $value)) $taxonomy = key($tax);
    }
  }
echo($taxonomy); // this print "sgd"
Xriuk 10-Feb-2018 11:52
Be careful when search for indexes from array_keys() if you have a mixed associative array it will return both strings and integers resulting in comparison errors

<?php
// This syntax is allowed by PHP
$arr = array(
 
"nice",
 
"car" => "fast",
 
"none"
);

// Print the keys
var_dump(array_keys($arr));

/* The above prints this, as you can see we have mixed keys
array(3) {
  [0]=>
  int(0)
  [1]=>
  string(3) "car"
  [2]=>
  int(1)
}
*/

// Search for the index "car"
var_dump(array_search("car", array_keys($arr))); // Prints "int(0)" which is WRONG
?>

This happens because PHP, when comparing strings and integers, casts strings TO integers and this results in most of the cases in string becoming 0, so that's why when array_search() compares the first index (0) with the key "car" it gets true because apparently ("car" == 0) IS TRUE.
Setting array_search() to strict mode won't solve the problem because then array_search("0", array_keys($arr)) would return false even if an element with index 0 exists.
So my solution just converts all indexes from array_keys() to strings and then compares them correctly:

<?php
var_dump
(array_search("car", array_map("strval", array_keys($arr)))); // Prints "int(1)" which is CORRECT
?>
thinbegin at gmail dot com 18-Jan-2018 10:18
Despite PHP's amazing assortment of array functions and juggling maneuvers, I found myself needing a way to get the FULL array key mapping to a specific value. This function does that, and returns an array of the appropriate keys to get to said (first) value occurrence.

function array_recursive_search_key_map($needle, $haystack) {
    foreach($haystack as $first_level_key=>$value) {
        if ($needle === $value) {
            return array($first_level_key);
        } elseif (is_array($value)) {
            $callback = array_recursive_search_key_map($needle, $value);
            if ($callback) {
                return array_merge(array($first_level_key), $callback);
            }
        }
    }
    return false;
}

usage example:
-------------------

$nested_array = $sample_array = array(
    'a' => array(
        'one' => array ('aaa' => 'apple', 'bbb' => 'berry', 'ccc' => 'cantalope'),
        'two' => array ('ddd' => 'dog', 'eee' => 'elephant', 'fff' => 'fox')
    ),
    'b' => array(
        'three' => array ('ggg' => 'glad', 'hhh' => 'happy', 'iii' => 'insane'),
        'four' => array ('jjj' => 'jim', 'kkk' => 'kim', 'lll' => 'liam')
    ),
    'c' => array(
        'five' => array ('mmm' => 'mow', 'nnn' => 'no', 'ooo' => 'ohh'),
        'six' => array ('ppp' => 'pidgeon', 'qqq' => 'quail', 'rrr' => 'rooster')
    )
);

$search_value = 'insane';

$array_keymap = array_recursive_search_key_map($search_value, $nested_array);

var_dump($array_keymap);
// Outputs:
// array(3) {
// [0]=>
//  string(1) "b"
//  [1]=>
//  string(5) "three"
//  [2]=>
//  string(3) "iii"
//}

----------------------------------------------

But again, with the above solution, PHP again falls short on how to dynamically access a specific element's value within the nested array. For that, I wrote a 2nd function to pull the value that was mapped above.

function array_get_nested_value($keymap, $array)
{
    $nest_depth = sizeof($keymap);
    $value = $array;
    for ($i = 0; $i < $nest_depth; $i++) {
        $value = $value[$keymap[$i]];
    }

    return $value;
}

usage example:
-------------------
echo array_get_nested_value($array_keymap, $nested_array);   // insane
t-fav 19-Oct-2017 01:20
Noted some interesting behaviour when using array_search to find the correct index in an array containing an index with a value of 0.

The following works as expected.

<?php

$dataSet
= [];
$dataSet[] = ['name' => 'Row A', 'items' => ['ItemA' => 1, 'ItemB' => 2]];
$dataSet[] = ['name' => 'Row B', 'items' => ['ItemA' => 1, 'ItemB' => 2]];

$rows = [];
foreach (
$dataSet as $dataRow) {
   
$row = [
       
$dataRow['name'],
       
'ItemA',
       
'ItemB'
   
];
   
    foreach (
$dataRow['items'] as $key => $value) {
       
$index = array_search($key, $row);
       
$row[$index] = $value;
    }
   
   
$rows[] = $row;
}

print_r($rows);

It returns:

Array
(
    [
0] => Array
        (
            [
0] => Row A
           
[1] => 1
           
[2] => 2
       
)
    [
1] => Array
        (
            [
0] => Row B
           
[1] => 1
           
[2] => 2
       
)
)

However, the following seems to totally ignore the index containing the value 0 and thus returns the wrong index.

<?
php

$dataSet
= [];
$dataSet[] = ['name' => 'Row A', 'items' => ['ItemA' => 1, 'ItemB' => 2]];
$dataSet[] = ['name' => 'Row B', 'items' => ['ItemA' => 0, 'ItemB' => 2]];

$rows = [];
foreach (
$dataSet as $dataRow) {
   
$row = [
       
$dataRow['name'],
       
'ItemA',
       
'ItemB'
   
];
   
    foreach (
$dataRow['items'] as $key => $value) {
       
$index = array_search($key, $row);
       
$row[$index] = $value;
    }
   
   
$rows[] = $row;
}

print_r($rows);

It returns:

Array
(
    [
0] => Array
        (
            [
0] => Row A
           
[1] => 1
           
[2] => 2
       
)
    [
1] => Array
        (
            [
0] => Row B
           
[1] => 2
           
[2] => ItemB
       
)
)

However, setting ItemA's value on Row B to '' or false works fine. Also, passing in the third optional parameter [, bool $strict = true ] works correctly as well. Is this intentional behaviour?
justeme at sina dot com 25-Jul-2017 08:42
/*
Be careful!!!
when i in php7.1.5
below program return int 0
is it a bug?
*/
array_search(0,['a','b','c'])
mohamedn085 at gmail dot com 20-Jun-2017 04:59
Mohamed Nabil

hallo every body This function matches two arrays like
search an array like another or not  array_match which can match

function array_match($arr1,$arr2,$highlevel=false)
{

   $keys1=array_keys($arr1);
   $keys2=array_keys($arr2);

   $values1=array_values($arr1);
   $values2=array_values($arr2);
   
     if ($highlevel)
     {
    
     if ($keys1 === $keys2 && $values1 === $values2)
     {
        
         return true;

     }
       return false;
     }

     if ($keys1 == $keys2 && $values1 == $values2)
     {
        
         return true;

     }
       return false;
   
}
 var_dump(array_match(['id'=>'1','username'=>'2'],['id'=>'1','username'=>2]));
  //Output:true

 var_dump(array_match(['id'=>'1','username'=>'2'],['id'=>'1','username'=>2],true));
 //Output:false
turabgarip at gmail dot com 10-Mar-2017 03:04
About searcing in multi-dimentional arrays; two notes on "xfoxawy at gmail dot com";

It perfectly searches through multi-dimentional arrays combined with array_column() (min php 5.5.0) but it may not return the values you'd expect.

<?php array_search($needle, array_column($array, 'key')); ?>

Since array_column() will produce a resulting array; it won't preserve your multi-dimentional array's keys. So if you check against your keys, it will fail.

For example;

<?php
$people
= array(
 
2 => array(
   
'name' => 'John',
   
'fav_color' => 'green'
 
),
 
5=> array(
   
'name' => 'Samuel',
   
'fav_color' => 'blue'
 
)
);

$found_key = array_search('blue', array_column($people, 'fav_color'));
?>

Here, you could expect that the $found_key would be "5" but it's NOT. It will be 1. Since it's the second element of the produced array by the array_column() function.

Secondly, if your array is big, I would recommend you to first assign a new variable so that it wouldn't call array_column() for each element it searches. For a better performance, you could do;

<?php
$colors
= array_column($people, 'fav_color');
$found_key = array_search('blue', $colors);
?>
maciej at speccode dot com 18-Aug-2015 05:58
FYI, remember that strict mode is something that might save you hours.

If you're searching for a string and you have a "true" boolean on the way - you will get it as result (first occurrence). Example below:

<?php

$arr
= [
   
'foo'    => 'bar',
   
'abc'    => 'def',
   
'bool'   => true,
   
'target' => 'xyz'
];

var_dump( array_search( 'xyz', $arr ) ); //bool
var_dump( array_search( 'xyz', $arr, true ) ); //target

?>
xfoxawy at gmail dot com 02-Feb-2015 11:30
in (PHP 5 >= 5.5.0) you don't have to write your own function to search through a multi dimensional array

ex :

$userdb=Array
(
    (0) => Array
        (
            (uid) => '100',
            (name) => 'Sandra Shush',
            (url) => 'urlof100'
        ),

    (1) => Array
        (
            (uid) => '5465',
            (name) => 'Stefanie Mcmohn',
            (pic_square) => 'urlof100'
        ),

    (2) => Array
        (
            (uid) => '40489',
            (name) => 'Michael',
            (pic_square) => 'urlof40489'
        )
);

simply u can use this

$key = array_search(40489, array_column($userdb, 'uid'));
nan at designpromote dot co dot uk 16-Jan-2015 04:45
$array = ['a', 'b', 'c'];
$key = array_search('a', $array); //$key = 0
if ($key)
{
//even a element is found in array, but if (0) means false
//...
}

//the correct way
if (false !== $key)
{
//....
}

It's what the document stated "may also return a non-Boolean value which evaluates to FALSE."
dk at danilok dot com 21-Aug-2014 06:26
I needed a way to return the value of a single specific key, thus:

<?php

function recursive_return_array_value_by_key($needle, $haystack){
   
$return = false;
    foreach(
$haystack as $key => $val){
        if(
is_array($val)){
           
$return = recursive_return_array_value_by_key($needle, $val);
        }
        else if(
$needle === $key){
            return
"$val\n";
        }
    }
    return
$return;
}

?>
sunelbe at gmail dot com 21-Sep-2012 12:19
hey i have a easy multidimensional array search function

<?php
function search($array, $key, $value)
{
   
$results = array();

    if (
is_array($array))
    {
        if (isset(
$array[$key]) && $array[$key] == $value)
           
$results[] = $array;

        foreach (
$array as $subarray)
           
$results = array_merge($results, search($subarray, $key, $value));
    }

    return
$results;
}
?>
sneskid at hotmail dot com 15-Mar-2012 08: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
?>
revoke 11-Oct-2011 04: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 02: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;
        }
    }
}
?>
stefano@takys dot it 14-Jan-2011 01:58
for searching case insensitive better this:

<?php
array_search
(strtolower($element),array_map('strtolower',$array));
?>
codeslinger at compsalot dot com 02-Oct-2009 12: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];
  }

?>
buddel 08-Jun-2009 08: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 12: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 10: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.
helenadeus at gmail dot com 25-Jan-2009 01: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);

   
}

?>
azaozz, gmail 20-Dec-2008 05: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
dcez at land dot ru 30-Apr-2008 01: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 03-Apr-2008 04: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)
}
kermes [at] thesevens [dot] net 11-Sep-2007 03: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 01-Sep-2007 11: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
mark dot php at mhudson dot net 11-Sep-2006 05: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()
RichGC 20-Mar-2006 05: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
?>
F?lix Cloutier <felixcca at yahoo dot ca> 22-Oct-2004 05: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... ;)
cue at openxbox dot com 09-Jun-2003 07: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]);
}
?>
PHP8中文手册 站长在线 整理 版权归PHP文档组所有