duandian8110
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
    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;
        }
    
    点赞 评论

相关推荐