PHP使用键值组合两个数组

时间:2016-10-17 09:03:41

标签: php arrays merge

这个问题可能听起来很愚蠢,但我没有找到好的方法。

我必须使用这样的多维数组: -

array 
(
  [0] => Array
      (
        [field1] => A
        [field2] => 100
        [field3] => 20
      )

  [1] => Array
      (
        [field1] => B
        [field2] => 100
        [field3] => 30
      )
  [2] => Array
      (
        [field1] => C
        [field2] => 100
        [field3] => 30
      )
)



 array 
(
  [0] => Array
      (
        [field1b] => A
        [field2b] => 500
        [field3b] => 0
      )

  [1] => Array
      (
        [field1b] => B
        [field2b] => 300
        [field3b] => 10
      )
)

我想使用field1作为Key合并两个数组,并且要记住数组的大小不同。

结果数组应为:

array 
(
  [0] => Array
      (
        [field1] => A
        [field2] => 100
        [field3] => 20
        [field2b] => 500
        [field3b] => 0

      )

  [1] => Array
      (
        [field1] => B
        [field2] => 100
        [field3] => 30
        [field2b] => 300
        [field3b] => 10
      )
  [2] => Array
      (
        [field1] => C
        [field2] => 100
        [field3] => 30
      )
)

提前致谢!

4 个答案:

答案 0 :(得分:4)

试试这个,

$array1 =  array 
(
  "0" => Array
      (
        "field1" => "A",
        "field2" => "100",
        "field3" => "20"
      ),

  "1" => Array
      (
        "field1" => "B",
        "field2" => "100",
        "field3" => "30"
      ),
  "2" => Array
      (
        "field1" => "C",
        "field2" => "100",
        "field3" => "30"
      )
);



$array2 =  array 
(
  "0" => Array
      (
        "field1b" => "A",
        "field2b" => "500",
        "field3b" => "0"
      ),

  "1" => Array
      (
        "field1b" => "B",
        "field2b" => "300",
        "field3b" => "10"
      )
);

foreach($array1 as $key=>$val)
{
    $new_array[$key]['field1'] = $val['field1'];
    $new_array[$key]['field2'] = $val['field2'];
    $new_array[$key]['field3'] = $val['field3'];
    if (array_key_exists($key,$array2))//Check if the key is exists in an array
    {
      $new_array[$key]['field2b'] = $array2[$key]['field2b'];
      $new_array[$key]['field3b'] = $array2[$key]['field3b'];
    }       
}

echo "<pre>";
print_r($new_array);
echo "</pre>";

<强> DEMO

答案 1 :(得分:3)

非常简单: - 应用foreach并执行以下操作: -

<?php
$final_array = array();

foreach ($arr1 as $key=>$val){
   if(isset($arr2[$key])){
      $final_array[$key] = array_merge($val,$arr2[$key]);
      unset($final_array[$key]['field1b']);
   }else{
      $final_array[$key] = $val;
   }

}

echo "<pre/>";print_r($final_array);

输出: - https://eval.in/661803

如果您的两个数组都有不同数量的元素,那么: -

  <?php
  $count1 = count ($arr1);
  $count2 = count ($arr2);
  $final_array = array();  
  if ($count1 >$count2){
    foreach ($arr1 as $key=>$val){
       if(isset($arr2[$key])){
          $final_array[$key] = array_merge($val,$arr2[$key]);
          unset($final_array[$key]['field1b']);
       }else{
          $final_array[$key] = $val;
       }
    }
    echo "<pre/>";print_r($final_array);
  }else if ($count2 >$count1){
    foreach ($arr2 as $key=>$val){
       if(isset($arr1[$key])){
          $final_array[$key] = array_merge($val,$arr1[$key]);
          unset($final_array[$key]['field1b']);
       }else{
          $final_array[$key] = $val;
       }
    }
    echo "<pre/>";print_r($final_array);
  }else{
    foreach ($arr1 as $key=>$val){
       if(isset($arr2[$key])){
          $final_array[$key] = array_merge($val,$arr2[$key]);
          unset($final_array[$key]['field1b']);
       }else{
          $final_array[$key] = $val;
       }

    }
    echo "<pre/>";print_r($final_array);
  }

答案 2 :(得分:1)

MVVM

DEMO

你可以尝试一下。

答案 3 :(得分:0)

如何定义硬编码密钥条件呢?你需要的只是提供不应该包含在组合数组中的密钥..

Demo

<?php
$A        = array(
    array(
        'field1' => 'A',
        'field2' => '100',
        'field3' => '20',
    ),
    array(
        'field1' => 'B',
        'field2' => 100,
        'field3' => 30,
    ),
    array(
        'field1' => 'C',
        'field2' => 100,
        'field3' => 30,
    )
);
$B        = array(
    array(
        'field1b' => 'A',
        'field2b' => 500,
        'field3b' => 0
    ),
    array(
        'field1b' => 'B',
        'field2b' => 300,
        'field3b' => 10
    )
);
$combined = array();
foreach($A as $key => $arr) {
    $tempKey    = reset($arr);
    $second_arr = $B[ $key ];
    $tempKey2   = reset($B[ $key ]);
    if( $tempKey == $tempKey2 ) {
        $combined[] = $arr + $second_arr;
        unset( $combined[ $key ]['field1b'] );
    }
    else {
        $combined[] = $arr;
    }
}
echo '<pre>';
print_r($combined);
echo '</pre>';