You can use array_walk()
with anonymous function and array_unshift()
:
array_walk
(
$array1,
function( &$row, $key, $kind )
{
array_unshift( $row, $kind[$key] );
},
$array2
);
array_walk()
modify an array using a custom function. The callable function arguments are the array item (note that we have to set it by reference using &
), the array key (optional) and an optional custom parameter (in our case, $array2
). Inside the function, with array_unshift()
) we can prepend to each item the relative $array2
item, selecting it by key $key
.
- Read more about array_walk()
- Read more about array_unshift()
- Read more about anonymous functions
- Read more about passing by reference
4
solved Add array values at the start of each subArray from other array