I'm trying to use an array function (I thought about array_map()
or array_walk()
, but couldn't get it to do what I want) in order to create an array using a multidimensional array (Like a MySQL result) turning a field from the array into the key of the new one.
Say I have an array like this one:
$a = array(
0 => array( 'id' => 1, 'name' => 'john' ),
1 => array( 'id' => 28, 'name' => 'peter' )
);
And I'd like to get another array like this:
$b = array(
1 => array( 'name' => 'john' ),
28 => array( 'name' => 'peter' )
);
I can solve it with a simple foreach loop, but I wonder whether there's a more efficient way, using a built-in function.
array_map
and array_walk
don't allow you to change keys. A foreach loop is definitely the way to go. Foreach can even be more efficient than array_walk/array_map a lot of the time.