将两个数组合并为一个数组php

时间:2014-08-06 01:37:13

标签: php arrays

有没有办法合并这两个数组:

Array
(
    [green]  => avocado
    [red]    => apple
    [yellow] => banana
)

Array
(
    [brown]  => coconut
    [orange]    => orange
)

进入像这样的单维数组

Array
(
    [green]  => avocado
    [red]    => apple
    [yellow] => banana
    [brown]  => coconut
    [orange]    => orange
)

提前致谢!我刚刚开始编程,请在评估此问题时牢记这一点。

2 个答案:

答案 0 :(得分:1)

来自文档:

http://php.net/manual/en/function.array-merge.php

$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);

输出:

Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)

答案 1 :(得分:0)

您是否先查看了array_merge的php文档?

http://us2.php.net/manual/en/function.array-merge.php

基本上,

$merged_array = array_merge($first_array,$second_array);

满足您的需求。参数的数量也可以超过两个。