合并由共享列值相关的两个二维数组的关联行

问题描述 投票:0回答:4

我必须像这样的多维数组:

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 作为键合并两个数组,并记住数组的大小不同。

结果数组应该是:

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
      )
)
php arrays multidimensional-array merge
4个回答
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>";

演示


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);
  }

1
投票
<?php
$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"
      )
);

$new_array1 = [];
foreach ($array1 as $object1) {
  $new_array1[$object1['field1']] =  $object1;
}

$new_array2 = [];
foreach ($array2 as $key => $object2) {
  $new_array2[$object2['field1b']] =  $object2;
}
$new_array = [];
foreach($new_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,$new_array2))//Check if the key is exists in an array
    {
      $new_array[$key]['field2b'] = $new_array2[$key]['field2b'];
      $new_array[$key]['field3b'] = $new_array2[$key]['field3b'];
    }       
}

print_r($new_array);

演示

你可以尝试一下。


0
投票

定义硬编码键条件怎么样?您所需要的只是提供不应包含在组合数组中的密钥..

演示

<?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>';
© www.soinside.com 2019 - 2024. All rights reserved.