0

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']
]

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.