0

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
  )
)
3
  • Have you considered using foreach(){}? Commented Feb 27, 2019 at 18:24
  • [$array[0] + $array[1]] Commented Feb 27, 2019 at 18:30
  • foreach() keeps giving me the same thing. Commented Feb 27, 2019 at 18:30

2 Answers 2

3

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

Comments

1

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);

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.