For simple arrays with key value pairs, we can easily locate the key based on the value using array_search()
. But I have an array in which the values may be a string or an array, and need to find the key where the value is an array with specific keys.
$myArray = array(
0 => string_value,
1 => string_value2,
3 => array(
'config' => array(
'option1' => value1,
'option2' => value2,
),
),
4 => string_value3,
);
I need to find the key for the element where the child array has a key config
-- i.e. I should search for config and return 3
.
I'd prefer not to cycle through the array. Not a big deal if that's the only option. But I'm wondering if there's a more elegant way to locate that key.
use array_filter to filter the array with config.