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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵