duandian8110 2018-08-12 21:43
浏览 75
已采纳

php从嵌套的多维数组中删除重复项

I have a nested multi dimensional array - each array has a nested array called values which contains a array

it looks like this:

    Array
(
    [0] => Array
        (
            [id] => 53
            [delivery_partner_id] => 6
            [delivery_partner_product_id] => 37
            [parent_id] => 0
            [name] => group 1
            [description] => 
            [parent_option_type] => single
            [price_type] => not_set
            [price_sell] => 
            [price_purchase] => 
            [sort] => 1
            [created_at] => 2018-08-12 21:35:14
            [updated_at] => 2018-08-12 21:35:14
            [values] => Array
                (
                    [0] => Array
                        (
                            [id] => 54
                            [delivery_partner_id] => 6
                            [delivery_partner_product_id] => 37
                            [parent_id] => 53
                            [name] => option name
                            [description] => 
                            [parent_option_type] => not_set
                            [price_type] => free
                            [price_sell] => 0.00
                            [price_purchase] => 0.00
                            [sort] => 1
                            [created_at] => 2018-08-12 21:35:14
                            [updated_at] => 2018-08-12 21:35:14
                        )

                    [1] => Array
                        (
                            [id] => 55
                            [delivery_partner_id] => 6
                            [delivery_partner_product_id] => 37
                            [parent_id] => 53
                            [name] => option name
                            [description] => 
                            [parent_option_type] => not_set
                            [price_type] => free
                            [price_sell] => 0.00
                            [price_purchase] => 0.00
                            [sort] => 2
                            [created_at] => 2018-08-12 21:35:14
                            [updated_at] => 2018-08-12 21:35:14
                        )

                    [2] => Array
                        (
                            [id] => 56
                            [delivery_partner_id] => 6
                            [delivery_partner_product_id] => 37
                            [parent_id] => 53
                            [name] => option name
                            [description] => 
                            [parent_option_type] => not_set
                            [price_type] => free
                            [price_sell] => 0.00
                            [price_purchase] => 0.00
                            [sort] => 3
                            [created_at] => 2018-08-12 21:35:14
                            [updated_at] => 2018-08-12 21:35:14
                        )

                )

        )

    [1] => Array
        (
            [id] => 57
            [delivery_partner_id] => 6
            [delivery_partner_product_id] => 37
            [parent_id] => 0
            [name] => group 1
            [description] => 
            [parent_option_type] => single
            [price_type] => not_set
            [price_sell] => 
            [price_purchase] => 
            [sort] => 2
            [created_at] => 2018-08-12 21:35:14
            [updated_at] => 2018-08-12 21:35:14
            [values] => Array
                (
                    [0] => Array
                        (
                            [id] => 58
                            [delivery_partner_id] => 6
                            [delivery_partner_product_id] => 37
                            [parent_id] => 57
                            [name] => option name
                            [description] => 
                            [parent_option_type] => not_set
                            [price_type] => free
                            [price_sell] => 0.00
                            [price_purchase] => 0.00
                            [sort] => 1
                            [created_at] => 2018-08-12 21:35:14
                            [updated_at] => 2018-08-12 21:35:14
                        )

                    [1] => Array
                        (
                            [id] => 59
                            [delivery_partner_id] => 6
                            [delivery_partner_product_id] => 37
                            [parent_id] => 57
                            [name] => option name
                            [description] => 
                            [parent_option_type] => not_set
                            [price_type] => free
                            [price_sell] => 0.00
                            [price_purchase] => 0.00
                            [sort] => 2
                            [created_at] => 2018-08-12 21:35:14
                            [updated_at] => 2018-08-12 21:35:14
                        )

                    [2] => Array
                        (
                            [id] => 60
                            [delivery_partner_id] => 6
                            [delivery_partner_product_id] => 37
                            [parent_id] => 57
                            [name] => option name
                            [description] => 
                            [parent_option_type] => not_set
                            [price_type] => free
                            [price_sell] => 0.00
                            [price_purchase] => 0.00
                            [sort] => 3
                            [created_at] => 2018-08-12 21:35:14
                            [updated_at] => 2018-08-12 21:35:14
                        )

                )

        )

)
1

The problem is that it is possible to have same named groups with exactly the same values. But how can I make this unique based on the group values and the "values"?

I tried with array unique but the problem is the nested array with values from some reason this will not work. I'm trying to find another approach to do this.

It should check first if two groups exists with the same values, if that is true it should check the values array and check if both the values are the same. If all is the same, it should remove the duplicate.

  • 写回答

1条回答 默认 最新

  • douyanguo7964 2018-08-13 07:34
    关注

    Tried this, seems to work, altough you might wan't to rewrite it to something more efficient :

    function removeDupplicates($array) {
            $result = [];
    
            while(count($array) > 0) {
    
                $currentRecord = array_pop($array);
    
                $valueFound = false;
    
                foreach($array as $value) {
                    if ($value === $currentRecord) {
                        $valueFound = true;
                    }
                }
    
                if (!$valueFound) {
                    $result[] = $currentRecord;
                }
            }
    
            return $result;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置