开发者

Pass array of arrays to function as if it was individual arrays

开发者 https://www.devze.com 2023-03-08 00:46 出处:网络
I have a variable that contains an array. That array contains X number of nearly identical arrays that I want to merge together.

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 t开发者_如何转开发hrough a foreach loop? Or is it just late and I'm missing something really obvious?


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号