In a lucky coincidence, I needed to do almost the exact same thing earlier today. You can use array_map()
in combination with array_shift()
:
$foo = array_map('array_shift', &$arr);
$bar = array_map('array_shift', &$arr);
Note that $arr
is passed by reference! If you don't do that, then each time it would return the contents of $arr[<index>]['foo']
. However, again because of the reference - you won't be able to reuse $arr
, so if you need to do that - copy it first.
The downside is that your array keys need to be ordered in the same way as in your example, because array_shift()
doesn't actually know what the key is. It will NOT work on the following array:
$arr = array(
0 => array(
'foo' => '1',
'bar' => 'A'
),
1 => array(
'bar' => 'B',
'foo' => '2'
),
2 => array(
'foo' => '3',
'bar' => 'C'
)
);
Update:
After reading the comments, it became evident that my solution triggers E_DEPRECATED
warnings for call-time-pass-by-reference. Here's the suggested (and accepted as an answer) alternative by @Baba, which takes advantage of the two needed keys being the first and last elements of the second-dimension arrays:
$foo = array_map('array_shift', $arr);
$bar = array_map('array_pop', $arr);