测试代码:

$a1 = array('dbhost' => 'localhost', 'dbname' => 'nextblog', 'dbport' => 3306);
$a2 = array('dbuser' => 'root', 'dbpass' => 'pass', 'dbport' => 3305);
print_r(array_merge($a1, $a2));
print_r($a1 + $a2);

 

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

Array
(
    [dbhost] => localhost
    [dbname] => nextblog
    [dbport] => 3305
    [dbuser] => root
    [dbpass] => pass
)

 

方式二:使用数组相加方式,如果输入的数组中有相同的字符串键名,则该键名后面的值将被忽略。

Array
(
    [dbhost] => localhost
    [dbname] => nextblog
    [dbport] => 3306
    [dbuser] => root
    [dbpass] => pass
)