如何从单个数组变量合并多个数组?假设我在一个数组变量中有这个
这些都在一个变量中..
$array = array(array(1), array(2));
Array
(
[0] => 1
)
Array
(
[0] => 2
)
如何结束这个
Array
(
[0] => 1
[1] => 2
)
这是 javascript 的 PHP 等价物Function#apply
(从数组生成参数列表):
$result = call_user_func_array("array_merge", $input);
演示:http: //3v4l.org/nKfjp
这可能有效:
$array1 = array("item1" => "orange", "item2" => "apple", "item3" => "grape");
$array2 = array("key1" => "peach", "key2" => "apple", "key3" => "plumb");
$array3 = array("val1" => "lemon");
$newArray = array_merge($array1, $array2, $array3);
foreach ($newArray as $key => $value) {
echo "$key - <strong>$value</strong> <br />";
}
array_merge 可以完成这项工作
$array_meged = array_merge($a, $b);
评论后
如果固定索引,您可以使用:
$array_meged = array_merge($a[0], $a[1]);
更通用的解决方案:
$array_meged=array();
foreach($a as $child){
$array_meged += $child;
}
$resultArray = array_merge ($array1, $array1);
$result = array();
foreach ($array1 as $subarray) {
$result = array_merge($result, $subarray);
}
// Here it is done
好读的东西: http: //ca2.php.net/manual/en/function.array-merge.php
递归:
http://ca2.php.net/manual/en/function.array-merge-recursive.php
$arr1 = array(0=>1);
$arr2 = array(0=>2);
$merged = array_merge($arr1,$arr2);
print_r($merged);