将项目从一个数组添加到另一个数组

时间:2010-06-07 16:30:14

标签: php arrays

我有两个数组:

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中的两个缺失项(code_4code_5)添加到array1?

谢谢!

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 

3 个答案:

答案 0 :(得分:7)

答案 1 :(得分:1)

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

array_merge是更好的方法,但是

答案 2 :(得分:0)

如果你可以使用数组的键来识别值,那么这将起作用

$resulting_array = $array2 + $array1;