2013-06-04 66 views
0

我想聯合兩個數組,覆蓋重複鍵但不想重新索引所有索引。有沒有一些功能可以做到這一點?array_merge和array1 + array2之間的折衷

我需要兩者的聯合結構,所以每個關鍵字中的內容都無關緊要。

陣列1:

[820] 
    [name1] 
     [id] = "My Name" 
    [name2] 
     [id] = "Your Name" 
[830] 
    [id] = "Mothers Name" 

陣列2:

[815] 
    [name1] 
     [id] = "IDs Name" 
[820] 
    [name1] 
     [id] = "Names Name" 
    [id] = "Last ID ID" 

結果應該是

[815] 
    [name1] 
     [id] = //does not matter 
[820] 
    [name1] 
     [id] = //does not matter 
    [name2] 
     [id] = //does not matter 
    [id] = //does not matter 
[830] 
    [id] = //does not matter 

回答

0
$result = array(); 
foreach ($array1 as $key => $value){ 
    if(isset($array2[$key])) 
     $result[$key] = array_merge($value, $array2[$key]); 
    else 
     $result[$key] = $value; 
} 
foreach ($array2 as $key => $value){ 
    if(isset($array1[$key])) 
     $result[$key] = array_merge($value, $array1[$key]); 
    else 
     $result[$key] = $value; 
} 
+0

THX它每工作很好:) –

相關問題