php 从一个数组合并多个数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13544985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Merge multiple arrays from one array
提问by Osa
How to merge multiple arrays from a single array variable ? lets say i have this in onearray variable
如何从单个数组变量合并多个数组?假设我在一个数组变量中有这个
Those are in one variable .. $array = array(array(1), array(2));
这些在一个变量中.. $array = array(array(1), array(2));
Array
(
[0] => 1
)
Array
(
[0] => 2
)
how to end up with this
如何结束这个
Array
(
[0] => 1
[1] => 2
)
回答by John Dvorak
This is the PHP equivalent of javascript Function#apply(generate an argument list from an array):
这是 javascript 的 PHP 等价物Function#apply(从数组生成参数列表):
$result = call_user_func_array("array_merge", $input);
demo: http://3v4l.org/nKfjp
演示:http: //3v4l.org/nKfjp
回答by 1990rk4
This may work:
这可能有效:
$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 />";
}
回答by Tomá? Votruba
回答by Ivan Buttinoni
array_merge can do the job
array_merge 可以完成这项工作
$array_meged = array_merge($a, $b);
after the comment
评论后
If fixed indexs you can use:
如果固定索引,您可以使用:
$array_meged = array_merge($a[0], $a[1]);
A more generic solution:
更通用的解决方案:
$array_meged=array();
foreach($a as $child){
$array_meged += $child;
}
回答by Samuel Cook
$arr1 = array(0=>1);
$arr2 = array(0=>2);
$merged = array_merge($arr1,$arr2);
print_r($merged);
回答by E_p
$resultArray = array_merge ($array1, $array1);
$resultArray = array_merge ($array1, $array1);
$result = array();
foreach ($array1 as $subarray) {
$result = array_merge($result, $subarray);
}
// Here it is done
Something good to read: http://ca2.php.net/manual/en/function.array-merge.php
值得一读:http: //ca2.php.net/manual/en/function.array-merge.php
Recursive:
递归:
http://ca2.php.net/manual/en/function.array-merge-recursive.php
http://ca2.php.net/manual/en/function.array-merge-recursive.php
回答by xbonez
array_mergeis what you need.
array_merge是你所需要的。
$arr = array_merge($arr1, $arr2);
Edit:
编辑:
$arr = array_merge($arr1[0], $arr1[1]);

