$testing = array(
'Testing' => array(
'topic' => 'topic',
'content' => 'content'
)
);
$newTesting = array(
'Testing' => array(
'new' => 'new'
)
);
$testing = array_merge_recursive($testing, $newTesting);
将输出
array (
'Testing' => array (
'topic' => 'topic',
'content' => 'content',
'new' => 'new',
),
)
注:如果你想覆盖的东西,使用这种方法是行不通的。例如,以相同的初始$testing
数组,如果您有:
$newTesting = array(
'Testing' => array(
'content' => 'new content',
'new' => 'new'
)
);
$testing = array_merge_recursive($testing, $newTesting);
然后输出将是:
array (
'Testing' => array (
'topic' => 'topic',
'content' => array (
0 => 'content',
1 => 'content-override',
),
'new' => 'new',
),
)
但如果这是一个希望的行为,那么你说对了!
编辑:看看这里是否array_merge_recursive应该替换而不是增加新的元素相同的密钥:http://www.php.net/manual/en/function.array-merge-recursive.php#93905