如何通过php中的键值合并两个多维数组?

时间:2019-05-20 15:26:43

标签: php arrays multidimensional-array array-merge array-replace

我有两个数组

$arr1 = [
    ['month' => 1], 
    ['month' => 2], 
    ['month' => 3], 
    ['month' => 4]
];

$arr2 = [
    ['month' => 3, 'info' => 'Alpha'], 
    ['month' => 4, 'info' => 'Beta']
];

我尝试过array_mergearray_merge_recursive都没有像我期望的那样合并数组。

预期合并:

$arr3 = [
    ['month' => 1], 
    ['month' => 2], 
    ['month' => 3, 'info' => 'Alpha'], 
    ['month' => 4, 'info' => 'Beta']
];

实际使用array_merge_recursive的是:

$arr3 = array_merge_recursive($arr2, $arr1);

$arr3 = [
    ['month' => 3, 'info' => 'Alpha'], 
    ['month' => 4, 'info' => 'Beta']
    ['month' => 1], 
    ['month' => 2], 
    ['month' => 3], 
    ['month' => 4]
];

3 个答案:

答案 0 :(得分:3)

如果数组在月份关联,则可以使用array_replace。
首先,将数组与array_column关联,然后将$ arr1替换为$ arr2值。

$arr1 = [
    ['month' => 1], 
    ['month' => 2], 
    ['month' => 3], 
    ['month' => 4]
];

$arr2 = [
    ['month' => 3, 'info' => 'Alpha'], 
    ['month' => 4, 'info' => 'Beta'],
    ['month' => 1, 'info' => 'Gamma'],
];


$arr1 = array_column($arr1, null, 'month');
$arr2 = array_column($arr2, null, 'month');

$result = array_replace($arr1, $arr2);
var_dump($result);

返回:

array(4) {
  [1]=>
  array(2) {
    ["month"]=>
    int(1)
    ["info"]=>
    string(5) "Gamma"
  }
  [2]=>
  array(1) {
    ["month"]=>
    int(2)
  }
  [3]=>
  array(2) {
    ["month"]=>
    int(3)
    ["info"]=>
    string(5) "Alpha"
  }
  [4]=>
  array(2) {
    ["month"]=>
    int(4)
    ["info"]=>
    string(4) "Beta"
  }
}

https://3v4l.org/tpshB

如果您使用的PHP较旧版本没有array_column,则可以从手册中使用此legacy code

if (!function_exists('array_column')) {
    function array_column($array, $columnKey, $indexKey = null)
    {
        $result = array();
        foreach ($array as $subArray) {
            if (is_null($indexKey) && array_key_exists($columnKey, $subArray)) {
                $result[] = is_object($subArray)?$subArray->$columnKey: $subArray[$columnKey];
            } elseif (array_key_exists($indexKey, $subArray)) {
                if (is_null($columnKey)) {
                    $index = is_object($subArray)?$subArray->$indexKey: $subArray[$indexKey];
                    $result[$index] = $subArray;
                } elseif (array_key_exists($columnKey, $subArray)) {
                    $index = is_object($subArray)?$subArray->$indexKey: $subArray[$indexKey];
                    $result[$index] = is_object($subArray)?$subArray->$columnKey: $subArray[$columnKey];
                }
            }
        }
        return $result;
    }
}

答案 1 :(得分:1)

在这种情况下,我遍历数组并构造所需的输出。

$output = [];
$allMerged = array_merge($arr1, $arr2);
array_walk($allMerged, 
  function($item, $key) use (&$output) {
    print_r($item);
    if ( array_key_exists($item['month'], $output) ) {
        if ( isset($item['info']) ) {
            $output[$item['month']]['info'] = $item['info'];
        } else {
            $output[$item['month']] = $item;
        }
    } else {
        $output[$item['month']] = $item;
    }
});
print_r($output);

答案 2 :(得分:-1)

您可以尝试以下代码

$newArr = [];
foreach($arr1 as $row) {
    $newArr[$row['month']] = $row;
}
foreach($arr2 as $arr) {
    if(array_key_exists($arr['month'], $newArr)) {
        $newArr[$arr['month']]['info'] = $arr['info'];
    }
} 

print_r($newArr);