PHP基础之数组合并(array_merge())

数组合并(array_merge())应用于合并两个或两个以上的数组。 数组合并一共涉及到两类合并。 1. 当键名(key)不同时 2. 当键名(key)相同时 第一种情况,我们通常直接使用array_merge(). 首先创建一个config.php文件 <?phpreturn array(‘DB_NAME’=>’DATABASE’,‘DB_HOST’=>’LOCALHOST’,‘USERNAME’=>’admin’,‘address’=>’NYC’?> 在你需要合并上面数组的merge.php文件中,定义config.php为一个变量a,定义另外的数组为变量b <?php$a =  include ‘config.php’;$b = array(‘port’=>’3306’,‘address’=>’901itcom’);return array_merge($a,$b);?> 在方法中,只需要包含merge.php文件,既可以输出config.php和merge.php <?phpclass newAction {protected $c;function __construct(){$this->c =…