1

This seems simple yet I cannot figure it out. I need to return an array merge for 2 arrays, they are not exactly the same and one is a global multidimensional array.

$array1 = ['dogs' => __('Dogs'), 'cats' => __('Cats')];  // localized
$array2 = [
    'ducks' => ['width' => 350, 'height' => 350, 'crop' => true],
    'cows' => ['width' => 750, 'height' => 150, 'crop' => true],
];  // not localized

I need to $merge = array_merge($array1, $array2); to return an array like this.

array('dogs' => __('Dogs'), 'cats' => __('Cats'), 'ducks', 'cows');  

But I'm getting all kinds of weird results.

3 Answers 3

6

Try this:

$merge = array_merge($array1, array_keys($array2));
Sign up to request clarification or add additional context in comments.

Comments

0

try

$animalNames = array_keys($animals);

Comments

0

To append the keys of $array2 as new elements of $array1, use array_push() with the spread operator. This way you don't bother handling the elements of the first array; just add the new elements individually. Demo

array_push($array1, ...array_keys($array2));

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.