数组 函数
PHP Manual

array_merge

(PHP 4, PHP 5)

array_merge合并一个或多个数组

说明

array array_merge ( array $array1 [, array $array2 [, array $... ]] )

array_merge() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。

如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。然而,如果数组包含数字键名,后面的值将不会覆盖原来的值,而是附加到后面。

如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。

Example #1 array_merge() 例子

<?php
$array1 
= array("color" => "red"24);
$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
)

Example #2 简单的 array_merge() 例子

<?php
$array1 
= array();
$array2 = array(=> "data");
$result array_merge($array1$array2);
?>

别忘了数字键名将会被重新编号!

Array
(
    [0] => data
)

如果你想完全保留原有数组并只想新的数组附加到后面,用 + 运算符:

<?php
$array1 
= array();
$array2 = array(=> "data");
$result $array1 $array2;
?>

数字键名将被保留从而原来的关联保持不变。

Array
(
    [1] => data
)

Warning

array_merge() 的行为在 PHP 5 中被修改了。和 PHP 4 不同, array_merge() 现在只接受 array 类型的参数。不过可以用强制转换来合并其它类型。请看下面的例子。

Example #3 array_merge() PHP 5 例子

<?php
$beginning 
'foo';
$end = array(=> 'bar');
$result array_merge((array)$beginning, (array)$end);
print_r($result);
?>

以上例程会输出:

Array
(
    [0] => foo
    [1] => bar
)

参见 array_merge_recursive()array_combine()数组运算符

参数

array1

Initial array to merge.

...

Variable list of arrays to merge.

返回值

Returns the resulting array.

更新日志

版本 说明
5.0.0
Warning

The behavior of array_merge() was modified in PHP 5. Unlike PHP 4, array_merge() now only accepts parameters of type array. However, you can use typecasting to merge other types. See the example below for details.

Example #4 array_merge() PHP 5 example

<?php
$beginning 
'foo';
$end = array(=> 'bar');
$result array_merge((array)$beginning, (array)$end);
print_r($result);
?>

以上例程会输出:

    Array
    (
        [0] => foo
        [1] => bar
    )

范例

Example #5 array_merge() example

<?php
$array1 
= array("color" => "red"24);
$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
)

Example #6 Simple array_merge() example

<?php
$array1 
= array();
$array2 = array(=> "data");
$result array_merge($array1$array2);
?>

Don't forget that numeric keys will be renumbered!

Array
(
    [0] => data
)

If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator:

<?php
$array1 
= array(=> 'zero_a'=> 'two_a'=> 'three_a');
$array2 = array(=> 'one_b'=> 'three_b'=> 'four_b');
$result $array1 $array2;
var_dump($result);
?>

The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored.

array(5) {
  [0]=>
  string(6) "zero_a"
  [2]=>
  string(5) "two_a"
  [3]=>
  string(7) "three_a"
  [1]=>
  string(5) "one_b"
  [4]=>
  string(6) "four_b"
}

参见


数组 函数
PHP Manual