A temporary array keyed by name stores the values from the first two arrays. The temporary array is then copied to a final array keyed numerically:
$arr1 = array ( 'Jonah' => 27, 'Bianca' => 32 );
$arr2 = array ( 'Jonah' => 2, 'Bianca' => 7 );
$tmp = array();
// Using the first array, create array keys to $tmp based on
// the name, and holding the age...
foreach ($arr1 as $name => $age) {
$tmp[$name] = array('name' => $name, 'age' => $age);
}
// Then add the number from the second array
// to the array identified by $name inside $tmp
foreach ($arr2 as $name => $num) {
$tmp[$name]['number'] = $num;
}
// Final array indexed numerically:
$output = array_values($tmp);
print_r($output);
Array
(
[0] => Array
(
[name] => Jonah
[age] => 27
[number] => 2
)
[1] => Array
(
[name] => Bianca
[age] => 32
[number] => 7
)
)
Note: The last step of copying the array to make it numerically isn't strictly needed, if you're ok with your output array being keyed by name. In that case, $tmp
is the final product.