In_array for combined value ('test', 'value')

I am trying to use in_array or something like this for associative or more complex arrays.

This is a regular in_array

in_array('test', array('test', 'exists')); //true
in_array('test', array('not', 'exists')); // false

I'm trying to find a pair for example, a combination of "test" and "value". I can configure combos to search by array('test','value')or 'test'=>'value'as needed. But how can I do this search if the desired array is

array('test'=>'value', 'exists'=>'here');
or
array( array('test','value'), array('exists'=>'here') );
+3
source share
3 answers
if (
    array_key_exists('test', $array) && $array['test'] == 'value' // Has test => value
    ||
    in_array(array('test', 'value'), $array) // Has [test, value]
) {
    // Found
}
+3
source

If you want to see if there is a test key with a value of value, try the following:

<?php
$arr = array('key' => 'value', 'key2' => 'value');
if(array_key_exists('key',$arr) && $arr['key'] == 'value'))
     echo "It is there!";
else
     echo "It isn't there!";
?>
+1
source

, array_search()

, - , :

if (array_search(array('test','value'), array(array('test','value'),array('nottest','notvalue'))) !== false) {
    // item found...
}

..

, , :

If you just need to find out if a particular key / value pair is in the array, the easiest way to do this is:

<?php
if (isset($arr['key']) && $arr['key'] == 'value') { 
    // we have a match...
}
?>

if you need to find something in a more complex template, do not avoid creating a larger loop.

+1
source

All Articles