0

How can I search an array to know if there is a key=>value in array?

for example in the following array I want to know if [uid] => 10 exists or not.


Array ( 
    [0] => Array (
      [name] => a
      [uid] => 1 
    ) 
    [1] => Array (
      [name] => b
      [uid] => 2 
    ) 
  ) 
Nick.h
  • 4,035
  • 5
  • 21
  • 22
  • 1
    No there is no built-in function. You have to write your own. – Felix Kling Sep 17 '11 at 08:20
  • 1
    check here http://stackoverflow.com/questions/4128323/in-array-and-multidimensional-array – Gowri Sep 17 '11 at 08:26
  • @Felix Kling: What about `in_array`? – hakre Sep 17 '11 at 08:51
  • @hakre: Right, if the inner arrays do not contain any further values, then it could work. – Felix Kling Sep 17 '11 at 09:03
  • Right, as the question has changed, one might want to look for multiple key/value pairs as well, and some keys might not exist. I [added an answer](http://stackoverflow.com/questions/7453483/is-there-a-php-function-to-search-a-key-array-pair/7453880#7453880). – hakre Sep 17 '11 at 09:47

5 Answers5

2
$foundAt = null;
foreach($yourArray as $index => $pair){
    if($pair['uid']===10){
        $foundAt = $index;
        break;
    }
}
echo $foundAt;

$foundAt will be null if nothing was found, and an integer if it was found.

There's no built-in function for that specifically. I could only think of array_walk being useful here.

thwd
  • 23,956
  • 8
  • 74
  • 108
2

in_array and array_search work just fine with nested arrays:

$a = array(

    array('id' => 10),
    array('id' => 11),
    array('id' => 12),
    array('id' => 13),
    array('id' => 14),

);

var_dump(in_array(array('id' => 14), $a)); // true
var_dump(in_array(array('id' => 99), $a)); // false

var_dump(array_search(array('id' => 14), $a)); // 4
var_dump(array_search(array('id' => 99), $a)); // false
user187291
  • 53,363
  • 19
  • 95
  • 127
  • I wonder why nobody else noticed, was my first idea as well. +1 for simplicity. – hakre Sep 17 '11 at 08:52
  • @Nick.h: I [added an answer](http://stackoverflow.com/questions/7453483/is-there-a-php-function-to-search-a-key-array-pair/7453880#7453880) to reflect the new question that is having multiple values (which could mean as well in my eyes: to search for multiple values). – hakre Sep 17 '11 at 09:51
1
foreach($array as $index => $data){
   if ($data['uid'] === 10){
       echo $index;
       break;
   }
}
genesis
  • 50,477
  • 20
  • 96
  • 125
1

As you edited your question and there are more values inside each element to test for, array_intersect_assoc came to mind. It takes care of the cases where each element must not contain the key you're looking for.

Basically: If the intersection between an element and the needle is the needle, an element matches.

This comparison needs to be applied per each element, e.g. with a foreach. Wrapped into a function that function can return the key and FALSE if not found:

function array_search_array(array $haystack, array $needle)
{
    foreach($haystack as $key => $element)
    {
        if ($needle == array_intersect_assoc($element, $needle))
            return $key;
    }
    return FALSE;
}

The naming of the function is not optimal I must admit. Demo

This works as well if you need to search for multiple values (regardless of the order of those in $needle).

As long as you only need to search for a single key/value pair, the key should be checked prior accessing it. This is a modification of the code-example in the answer by Tom:

$foundAt = null;
foreach($yourArray as $index => $pair)
{
    if(isset($pair['uid']) && $pair['uid'] === 10)
    {
        $foundAt = $index;
        break;
    }
}
echo $foundAt;
Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
0

There is no built-in function but you can use something like this:

if (count(array_intersect($your_array, array("uid" => 10))) > 0)
    // pair exists

Breaking down the statement above:

Make an array with your preferred key and value:

$array_to_search = array("uid" => 10);

Use array_intersect() to get intersection of the arrays:

$result = array_intersect($your_array, $array_to_search);

Count the result elements:

if (count($result) > 0)
   // pair exists
chelmertz
  • 20,399
  • 5
  • 40
  • 46
fardjad
  • 20,031
  • 6
  • 53
  • 68