【问题标题】:How to merge two multi array without duplicates in PHP如何在PHP中合并两个不重复的多数组
【发布时间】:2021-11-28 20:37:15
【问题描述】:

我正在尝试合并两个多个数组。

两个数组都有相同的项({ "ort": "rom", "lioc": "inrom" })。

    $test_1 =  '
    {
        "hotels" : { 
            "hotel" : [
                { "ort": "rom", "lioc": "inrom" },
                { "ort": "paris", "lioc": "inpsrisd" },
                { "ort": "berlin", "lioc": "inberlin" },
                { "ort": "milano", "lioc": "inmilano" },
                { "ort": "paris", "lioc": "anotherinpsrisd" },
                { "ort": "muc", "lioc": "inmuc" }
            ]
        }
    }
    ';
    $test_2 =  '
    {
        "hotels" : { 
            "hotel" : [
                { "ort": "rom", "lioc": "inrom" },
                { "ort": "beijing", "lioc": "wanda" }
            ]
        }
    }
    ';
    
    $arr_test1 = json_decode($test_1, TRUE);
    $arr_test2 = json_decode($test_2, TRUE);

    var_dump(json_encode(array_merge_recursive($arr_test1, $arr_test2)));
    var_dump(json_encode(array_unique(array_merge($arr_test1,$arr_test2), SORT_REGULAR)));

如果我运行这段代码,我会得到以下结果。

result1:
{
    "hotels" : { 
        "hotel" : [
            { "ort": "rom", "lioc": "inrom" },
            { "ort": "paris", "lioc": "inpsrisd" },
            { "ort": "berlin", "lioc": "inberlin" },
            { "ort": "milano", "lioc": "inmilano" },
            { "ort": "paris", "lioc": "anotherinpsrisd" },
            { "ort": "muc", "lioc": "inmuc" },
            {"ort":"rom","lioc":"inrom"},
            {"ort":"beijing","lioc":"wanda"}
        ]
    }
}

result2:
{
    "hotels" : { 
        "hotel" : [
            { "ort": "rom", "lioc": "inrom" },
            { "ort": "beijing", "lioc": "wanda" }
        ]
    }
}

两者都不是我的预期结果。

正如您在此屏幕截图中看到的,{ "ort": "rom", "lioc": "inrom" } 项目在 result1 中重复。

关键是不知道数组的键是什么,也不知道数组的深度。

我想得到没有像这样重复项的合并数组。

{
    "hotels" : { 
        "hotel" : [
            { "ort": "rom", "lioc": "inrom" },
            { "ort": "paris", "lioc": "inpsrisd" },
            { "ort": "berlin", "lioc": "inberlin" },
            { "ort": "milano", "lioc": "inmilano" },
            { "ort": "paris", "lioc": "anotherinpsrisd" },
            { "ort": "muc", "lioc": "inmuc" },
            { "ort": "beijing", "lioc": "wanda" }
        ]
    }
}

我该如何解决这个问题?

【问题讨论】:

    标签: php arrays json


    【解决方案1】:

    array_merge_recursive 实际上是一个非常好的起点,但它不允许您使内部数组唯一。这可以通过使用您自己的递归唯一实现来实现。这种函数的一个例子

    function recursiveUnique($input): array
    {
        // if the input is not an array, just return the input
        if (!is_array($input)) {
            return $input;
        }
        // check if the array is a list or an associative array
        else if (count($input) === 0 || array_keys($input) === range(0, count($input) - 1)) {
            return array_values(array_unique($input, SORT_REGULAR));
        }
        else {
            foreach ($input as $key => $value) {
                $input[$key] = recursiveUnique($value);
            }
            return $input;
        }
    }
    

    如果您将递归合并的数组提供给此函数,您应该会得到您想要的结果。您可以在this 3v4l 中查看功能。

    【讨论】:

    • 感谢您关注我的老问题。我同意合并后删除重复的项目。但是,这种方式存在key-value(例如:“0”、“1”、...“5”、“7”)。我只想得到一个没有索引键的数组。关键是对数组不了解。怎么样?
    • @Silverstar 您可以通过将array_unique 包装在array_values 中来轻松实现这一目标。我调整了答案以反映这一点。这能回答你的问题吗?
    • 当然,您的函数比我已经创建和实现的函数更简单易懂。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2016-03-13
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    相关资源
    最近更新 更多