通过匹配子数组值合并2个关联数组?

时间:2017-12-16 08:24:28

标签: php mysql arrays

我需要通过键aa => "value"

合并这两个数组
$array1 = array(
    "aa" => array("RM","PR","LE"),
    "bb" => array("100a","200a","300a"),
    "cc" => array("1.99b","2.99b","3.99b")
);

第二个数组可能每个键有额外的值,在这个例子中注意“aa”键中的“ST”值。

$array2 = array(
    "aa" => array("RM","PR","ST","LE"),
    "dd"  => array("UK","UK","UK","UK"),
    "ee" => array("123","456","000","789")
);

最终成为这样的1个数组。

$final_array = array(
    "aa" => array("RM","PR","ST","LE"),
    "bb" => array("100a","200a",null,"300a"),
    "cc" => array("1.99b","2.99b",null,"3.99b")
    "dd"  => array("UK","UK","UK","UK"),
    "ee" => array("123","456","000","789"),
);

在所有位置产生空值。 它需要像MySQL连接一样处理,array_merge()array_merge_recursive()将无效。 有什么想法吗?提前谢谢。

3 个答案:

答案 0 :(得分:1)

假设$array2总是大于$array1且数组计数的差异等于1且比较在密钥'aa'

之间
// Get keys of each array, then merge, then get unique
$all_keys = array_unique(array_merge(array_keys($array1), array_keys($array2)));
// Get difference of 'aa' then the key
$diff_key = key(array_diff($array2['aa'], $array1['aa']));
// Set `$final_array` as equal to `$array1`
$final_array = $array1;
// Loop thru each keys
foreach ($all_keys as $key) {
    // Check if `$array1` has the key
    if (isset($array1[$key])) {
        // Check if `$array2` also has the key
        if (isset($array2[$key])) {
            // Splice array to add element of `$array2` to final array
            array_splice($final_array[$key], $diff_key, 0, $array2[$key][$diff_key]);
        } else {
            // Splice array to add empty element
            array_splice($final_array[$key], $diff_key, 0, '');
        }
    } else {
        // Add `$array2` element to `$final_array`
        $final_array[$key] = $array2[$key];
    }
}


print_r($final_array);

答案 1 :(得分:0)

尝试使用你的逻辑

$array1 = array(
"aa" => array("RM","PR","LE"),
"bb" => array("100a","200a","300a"),
"cc" => array("1.99b","2.99b","3.99b")
);
$array2 = array(
  "aa" => array("RM","PR","ST","LE"),
  "dd"  => array("UK","UK","UK","UK"),
  "ee" => array("123","456","000","789")
);

 $arr=array_merge_recursive($array1,$array2);
 $new_arr=array();
 foreach($arr as $key=>$val){
   $new_arr[$key]=array_unique($val);
 }

 echo "<pre>";
 print_r($new_arr);

答案 2 :(得分:0)

试试这个

$len1 =  count($array1['aa']);
$len2 =  count($array2['aa']);
$final_array = array();
for($i = 0; $i < $len2; $i++){
  $final_array['aa'][$i] = $array2['aa'][$i];
  if($array2['aa'][$i] != $array1['aa'][$i]){
    $final_array['bb'][$i] = 'null';
    $final_array['cc'][$i] = 'null';
    $final_array['dd'][$i] = $array2['dd'][$i];
    $final_array['ee'][$i] = $array2['ee'][$i];

  }else{
     $final_array['bb'][$i] = $array1['bb'][$i];
     $final_array['cc'][$i] = $array1['cc'][$i];
      $final_array['dd'][$i] = $array2['dd'][$i];
    $final_array['ee'][$i] = $array2['ee'][$i];
  }
  if($array2['aa'][$i] == $array1['aa'][$i-1]){
    $final_array['bb'][$i] = $array1['bb'][$i-1];
    $final_array['cc'][$i] = $array1['cc'][$i-1];
  }
}
print_r($final_array);

签入实时演示:https://eval.in/920223

OUTPUT如下:

Array
(
    [aa] => Array
        (
            [0] => RM
            [1] => PR
            [2] => ST
            [3] => LE
        )

    [bb] => Array
        (
            [0] => 100a
            [1] => 200a
            [2] => null
            [3] => 300a
        )

    [cc] => Array
        (
            [0] => 1.99b
            [1] => 2.99b
            [2] => null
            [3] => 3.99b
        )

    [dd] => Array
        (
            [0] => UK
            [1] => UK
            [2] => UK
            [3] => UK
        )

    [ee] => Array
        (
            [0] => 123
            [1] => 456
            [2] => 000
            [3] => 789
        )

)