php数组合并

数字索引为下标,并且两个数组键名不同

array_merge()

$arr1 = array(3=>'php',6=>'mysql',9=>'apache');
$arr2 = array('php','mysql','apache');
print_r(array_merge($arr1,$arr2));
结果是:
Array
(
    [0] => php
    [1] => mysql
    [2] => apache
    [3] => php
    [4] => mysql
    [5] => apache
)

键名会以连续方式重新索引。

"+" 号实现

$arr1 = array(3=>'php',6=>'mysql',9=>'apache');
$arr2 = array('php','mysql','apache');
print_r($arr1+$arr2);
结果是:
Array
(
    [3] => php
    [6] => mysql
    [9] => apache
    [0] => php
    [1] => mysql
    [2] => apache
)

数字索引为下标,两个数组键名相同的情况

"+"号

$arr1 = array('php','mysql','apache');
$arr2 = array('java','oracle','nginx');
print_r($arr1+$arr2);
结果是:
Array
(
    [0] => php
    [1] => mysql
    [2] => apache
)

array_merge()

print_r(array_merge($arr1,$arr2));
结果是:
Array
(
    [0] => php
    [1] => mysql
    [2] => apache
    [3] => java
    [4] => oracle
    [5] => nginx
)

非索引为下标,两个数组键名相同的情况

array_merge()

$arr1 = array('lan'=>'php','db'=>'mysql','server'=>'apache');
$arr2 = array('lan'=>'java','db'=>'oracle','server'=>'nginx');
print_r(array_merge($arr1,$arr2));
结果是:
Array
(
    [lan] => java
    [db] => oracle
    [server] => nginx
)

"+" 号

print_r($arr1+$arr2);
Array
(
    [lan] => php
    [db] => mysql
    [server] => apache
)

你可能感兴趣的:(php数组合并)