2012-11-24 78 views
-3

我需要將關聯數組合併到另一個關聯數組中。我知道PHP的array_merge,但它返回一個新的數組。這不是我想要的。將數組合併到另一個數組中

$ar1 = array('one'=>1, 'two'=>2, 'three'=>3); 
$ar2 = array('four'=>4, 'five'=>5); 

我想知道有沒有我可以使用合併AR2 $到$ AR1一個PHP函數。結果應該是,

$ar1 = array('one'=>1, 'two'=>2, 'three'=>3, 'four'=>4, 'five'=>5); 
+2

你就不能使用'array_merge'這樣便:'$ AR1 = array_merge($ AR1,AR2 $);' – Cyclonecode

+0

不使用array_merge你必須寫一個將它們手動組合的循環。爲什麼你不能有一個新的陣列? –

+0

@KristerAndersson是的:)。我對我感到羞恥。無論如何感謝 – Gihan

回答

2
$ar1 = array('one'=>1, 'two'=>2, 'three'=>3); 
$ar2 = array('four'=>4, 'five'=>5); 

array_merge使用

$array3 = array_merge($ar1,$ar2); 

它將合併2陣列並將其存儲在$array3。您也可以使用$ar1

工作示例http://codepad.viper-7.com/KzCHIB

0
$ar1 = array('one'=>1, 'two'=>2, 'three'=>3); 
$ar2 = array('four'=>4, 'five'=>5); 

$ar1 = array_merge($ar1, $ar2); 

print_r($ar1); 
0

最簡單的方式是array_merge的輸出分配給您的第一陣列。 這是你想要

<?php 
$ar1 = array('one'=>1, 'two'=>2, 'three'=>3); 
$ar2 = array('four'=>4, 'five'=>5); 

$ar1 = array_merge($ar1,$ar2); 

print_r($ar1); 
?> 
相關問題