Reputation: 67
Has anyone an idea how to replace the shortcuts in first array with values from second array?
$first_array = [
'product 1' => ['name 1', 'shortcut 1'],
'product 2' => ['name 2', 'shortcut 2']
];
$second_array = [
'product 1' => ['shortcut 1' => 'text 1'],
'product 2' => ['shortcut 2' => 'text 2']
];
// Do not work.
$new_array = array_replace($first_array, $second_array);
I Need output like this
$new_array = [
'product 1' => ['name 1' => 'text 1'],
'product 2' => ['name 2' => 'text 2']
]
Upvotes: 0
Views: 27
Reputation: 16436
Try to replace it with foreach loop with checking key and value
$first_array = [
'product 1' => ['name 1' => 'shortcut 1'],
'product 2' => ['name 2' => 'shortcut 2']
];
$second_array = [
'product 1' => ['shortcut 1' => 'text 1'],
'product 2' => ['shortcut 2' => 'text 2']
];
$new_array = array();
foreach($first_array as $key=>$value)
{
foreach($value as $key1=>$value1)
{
if(isset($second_array[$key][$value1]))
$new_array[$key][$key1]=$second_array[$key][$value1];
}
}
print_r($new_array);
Upvotes: 1