phparraysmultidimensional-arraymappingarray-key

Use values of a flat array as the keys in every row of a 2d array


I have a new PHP problem. I have 2 arrays, and I want a third array which is a combination of the first 2. The first array, $arr1, is something like this:

Array (
    [0] => name [1] => age
)

The second array, $arr2, is something like this:

Array ( 
    [0] => Array( [0] => Dave [1] => 20 )
    [1] => Array( [0] => Steve [1] => 25 )
    [2] => Array( [0] => Ace [1] => 23 ) 
)

And my idea is to create a new array, called $arr3, which should like this:

Array ( 
    [0] => Array( [name] => Dave [age] => 20 )
    [1] => Array( [name] => Steve [age] => 25 )
    [2] => Array( [name] => Ace [age] => 23 ) 
)

Can anyone tell me how to do this?


Solution

  • $arr3 = array();
    foreach ($arr2 as $person) {
        $arr3[] = array_combine($arr1, $person);
    }