Thomas
Thomas

Reputation: 117

Merging ONE Multidimensional Array

I have one multidimensional array I need to merge. This one has two pockets, future arrays could have four or six. All solutions I find start with two arrays, but I only have one. This doesn't seem like it should be difficult, but I can't find a solution.

I need this:

Array (
  [0] => Array (
    [51] => 1
    [52] => 1
  )

  [1] => Array (
    [75] => 1
    [76] => 1
  )
)

To be this:

Array (
  [0] => Array (
    [51] => 1
    [52] => 1
    [75] => 1
    [76] => 1
  )
)

Upvotes: 0

Views: 38

Answers (2)

Nigel Ren
Nigel Ren

Reputation: 57121

Using the Argument unpacking operator ... you can create a new array with the array_replace()...

$array = [[51 =>1 , 52=> 1], [75 =>1 , 76=> 1]];

$output = [array_replace([], ...$array)];

Upvotes: 3

AbraCadaver
AbraCadaver

Reputation: 78994

If the keys are unique and you want to keep them:

$result = call_user_func_array('array_replace', $array);

If the keys aren't unique or you don't care if they are reset (they will be reset):

$result = call_user_func_array('array_merge', $array);

Upvotes: 1

Related Questions