Skip to content
Advertisement

in_array for a combo value (‘test’,’value’)

I’m trying to use in_array or something like it for associative or more complex arrays.

This is the normal in_array

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

What I’m trying to search is a pair, like the combination ‘test’ and ‘value’. I can set up the combo to be searched to array('test','value') or 'test'=>'value' as needed. But how can I do this search if the array to be searched is

array('test'=>'value', 'exists'=>'here');
or
array( array('test','value'), array('exists'=>'here') );

Advertisement

Answer

if (
    array_key_exists('test', $array) && $array['test'] == 'value' // Has test => value
    ||
    in_array(array('test', 'value'), $array) // Has [test, value]
) {
    // Found
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement