2

I have a variable that contains an array. That array contains X number of nearly identical arrays that I want to merge together.

But since they're already in an array variable, I can't pass it to array_merge() like this:

array_merge( $AoA );

What I'd like to do is this, but I can't because I don't know how many items are in the array:

array_merge(
  $AoA[0],
  $AoA[1],
  $AoA[2],
  // etc ...
);

Is there a short hand I can use to accomplish this without having to run them through a foreach loop? Or is it just late and I'm missing something really obvious?

1 Answer 1

4

Use call_user_func_array(), passing in array_merge as a string and your array of arrays, which the function then treats as an array of arguments to the function:

call_user_func_array('array_merge', $AoA);
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.