$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