phparraysmerge

Append the keys of an associative array as values of another array


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.


Solution

  • Try this:

    $merge = array_merge($array1, array_keys($array2));