Given this array and a name (e.g. ‘def’), how do I get the containing array, or key?
$all = array(
'0' => array(
'name' => 'abc'
'option' => 1,
),
'1' => array(
'name' => 'def'
'option' => 1,
),
'2' => array(
'name' => 'ghi'
'option' => 0,
),
);
What’s the best way to return this array given ‘def’?
$single = array(
'name' => 'def'
'option' => 1,
);
I could do something like this:
$single = array();
foreach ($all as $key => $value) {
if ($value['name'] == 'def') {
$single = $all[$key];
}
}
Or prerender the keys in the array so that it looks like this:
$all = array(
'abc' => array(
'name' => 'abc'
'option' => 1,
),
'def' => array(
'name' => 'def'
'option' => 1,
),
'ghi' => array(
'name' => 'ghi'
'option' => 0,
),
);
$single = $all['def'];
But I’m wondering if there’s a shorter php function for that.
You could use array_filter: