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++ /QT 内存权限的判断函数列举
  • ¥15 深度学习GFnet理解问题
  • ¥15 单细胞小提琴堆叠图代码
  • ¥80 LS dyna mpp并行报错
  • ¥50 升级strust2版本到2.3.15.1后使用ognl3.0.6.jar windows环境中没有问题,但部署到linux环境报错
  • ¥15 vue页面,node封装接口
  • ¥15 求TMS320F280039C工程模板!
  • ¥15 delphi+fastreport实现分组补空打印问题
  • ¥15 使用python把两台mysql数据库服务器数据导出和导入
  • ¥15 NodeBB论坛配置Apache Solr中文搜索引擎的详细教程