如何合并下面的数组
Array
(
[0] => 14
)
Array
(
[0] => 18
)
并获得输出
Array
(
[0] => 14
[1] => 18
)
我使用下面的代码但不适合我
$result = call_user_func_array("array_merge", $input);
使用array_merge
方法:http://php.net/manual/en/function.array-merge.php
<?php
$array = array("1" );
$otherArray = array("2");
$result = array_merge($array, $otherArray);
print_r($result);
看现场代码:http://sandbox.onlinephpfunctions.com/code/7fb9da6de4fa9d0a8b220a06d2a59e9007655df1
肯定只是array_merge
就足够了?
$a=[14];
$b=[18];
$c=[23];
$d=[44];
$e=[27];
$f=[31];
$g=[99];
$out=array_merge($a,$b,$c,$d,$e,$f,$g);
printf('<pre>%s</pre>',print_r($out,true));
这将输出:
Array
(
[0] => 14
[1] => 18
[2] => 23
[3] => 44
[4] => 27
[5] => 31
[6] => 99
)
OP询问是否合并来自一个数组的数组。我们不知道那里有多少个数组,所以我们需要使用array_walk_recursive函数。在匿名函数中通过引用(!)传递来自公共范围的$ result,并将所有内部数组值存储在其中
$a = [[1], [2, 3]];
$result = [];
array_walk_recursive($a, function($value) use (&$result){
$result[] = $value;
});
var_dump($result);
为什么call_user_func?
$a = array(14);
$b = array(18);
$merged = array_merge($a,$b);