You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP中如何递归合并多维数组?替代array_merge实现层级无重复合并

PHP 深度合并数组:合并用户时间且不重复颜色值

问题背景

使用array_merge()合并两个数组时,仅会将新数组追加到现有数组末尾,无法处理下层的用户时间合并逻辑,也无法保证同分组下的color值不重复。需要实现以下合并规则:

  • 相同分组键(如A、B)的子数组合并时,保留color值(确保同分组color一致)
  • 合并user下的用户数据:新增用户直接加入,已有用户则合并时间列表并去重
  • 新增的分组键(如C)直接加入结果数组

现有数组数据

$existingArr = [
    "A" => [
        "color" => 'red',
        "user" => [
            "Daniel" => ["01:18:08", "04:10:12"],
            "Max" => ["01:04:00"],
            "Serto" => ["02:00:02"],
        ]
    ],
    "B" => [
        "color" => 'blue',
        "user" => [
            "Franz" => ["08:40:52"],
            "Hugo" => ["07:08:58"],
        ]
    ]
];

新数组数据

$newArr = [
    "A" => [
        "color" => 'red',
        "user" => [
            "Fabian" => ["06:03:00"], // 新增用户
            "Max" => ["04:10:12"], // 新增时间
            "Serto" => ["02:00:02"],
        ]
    ],
    "B" => [
        "color" => 'blue',
        "user" => [
            "Franz" => ["08:40:52", "14:05:32", "20:34:15"], // 合并已有时间并新增2个
            "Hugo" => ["04:10:12"], // 新增时间
        ]
    ],
    "C" => [ // 新增完整分组
        "color" => 'green',
        "user" => [
            "Maxi" => ["07:08:58", "04:10:12"],
        ]
    ]
];

解决方案:自定义递归合并函数

自定义一个函数来实现深度合并逻辑,针对user层的时间列表做去重合并,同时保留color值:

function mergeUserArrays($existing, $new) {
    $result = $existing;
    
    foreach ($new as $groupKey => $groupData) {
        // 如果分组已存在,合并用户数据
        if (isset($result[$groupKey])) {
            // 保留原color(假设同分组color一致,若需校验可在此添加判断)
            $result[$groupKey]['color'] = $existing[$groupKey]['color'];
            
            foreach ($groupData['user'] as $user => $times) {
                if (isset($result[$groupKey]['user'][$user])) {
                    // 合并时间并去重,保持原有顺序
                    $mergedTimes = array_unique(array_merge($result[$groupKey]['user'][$user], $times));
                    $result[$groupKey]['user'][$user] = $mergedTimes;
                } else {
                    // 新增用户直接加入
                    $result[$groupKey]['user'][$user] = $times;
                }
            }
        } else {
            // 新增分组直接加入结果
            $result[$groupKey] = $groupData;
        }
    }
    
    return $result;
}

// 执行合并
$resultArr = mergeUserArrays($existingArr, $newArr);

// 输出结果
echo '<pre>';
print_r($resultArr);
echo '</pre>';

执行结果

上述代码运行后将得到期望的结果数组:

$resultArr = [
    "A" => [
        "color" => 'red',
        "user" => [
            "Daniel" => ["01:18:08", "04:10:12"],
            "Fabian" => ["06:03:00"],
            "Max" => ["01:04:00", "04:10:12"],
            "Serto" => ["02:00:02"],
        ]
    ],
    "B" => [
        "color" => 'blue',
        "user" => [
            "Franz" => ["08:40:52", "14:05:32", "20:34:15"],
            "Hugo" => ["07:08:58", "04:10:12"],
        ]
    ],
    "C" => [
        "color" => 'green',
        "user" => [
            "Maxi" => ["07:08:58", "04:10:12"],
        ]
    ]
];

内容的提问来源于stack exchange,提问作者Cem Firat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 06:35:17