Let\'s assume I have an array of elements, which are arrays themselves, like so:
$array = [
[\'foo\' => \'ABC\', \'bar\' => \'DEF\'],
[\'foo\'
Notice array-column can get index as well (third argument):
mixed $index_key = NULL
So just use as:
array_column($array, null, 'foo');
You can also do it with array_reduce
$new_array = array_reduce($array, function($carry, $item) {
$carry[$item['foo']] = $item;
return $carry;
}, []);
Here is one liner for your case,
$temp = array_combine(array_column($array, 'foo'), $array);
Working demo.
array_combine — Creates an array by using one array for keys and another for its values
array_column — Return the values from a single column in the input array