2010-06-07 198 views
0

我有兩個陣列:從一個陣列將項目添加到另一個

1)第一陣列:

array(

    [0] => array(
       [code] => code_1 
       [value] => xxx 
    [1] => array(
       [code] => code_2 
       [value] => xxx  
    [2] => array(
       [code] => code_3 
       [value] => xxx  

2)第二個數組:

array(

    [0] => array(
       [settingcode] => code_1 
       [value] => xxx 
    [1] => array(
       [settingcode] => code_2 
       [value] => xxx  
    [2] => array(
       [settingcode] => code_3 
       [value] => xxx 
    [3] => array(
       [settingcode] => code_4 
       [value] => xxx  
    [4] => array(
       [settingcode] => code_5 
       [value] => xxx  

如何可以添加兩個缺少從array2到array1的項目(code_4code_5)?

謝謝!

UPD。

對不起,我需要澄清一下這個問題。我需要的結果數組的樣子:

array(

    [0] => array(
       [code] => code_1 
       [value] => xxx 
    [1] => array(
       [code] => code_2 
       [value] => xxx  
    [2] => array(
       [code] => code_3 
       [value] => xxx 
    [3] => array(
       [code] => code_4 
       [value] => xxx  
    [4] => array(
       [code] => code_5 
       [value] => xxx 

回答

1
function add($from, $to) 
{ 
foreach($from as $key => $value) 
{ 
    if($to[$key] == "") 
     $to[$key] = $value; 
} 
return $to; 
} 

array_merge將是更好的辦法,但是

0

如果你可以使用數組的鍵標識值,那麼這將工作

$resulting_array = $array2 + $array1; 
相關問題