参见英文答案 > Turning multidimensional array into one-dimensional array 11个
我想转换
$array_of_arrays = [[1,2,3],[4,5,6],[7,8],[9,10]];
成
$array = [1,2,3,4,5,6,7,8,9,10];
我可以用
$array = [];
foreach($array_of_arrays as $one_array):
$array = array_merge($array,$one_array);
endforeach;
但是我有很长的数组,是否有任何函数(方法)可以在没有foreach循环的情况下执行此操作
解决方法:
使用call_user_func_array函数:
$array_of_arrays = [[1,2,3],[4,5,6],[7,8],[9,10]];
$result = call_user_func_array("array_merge", $array_of_arrays);
print_r($result);
输出:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)