代码之家  ›  专栏  ›  技术社区  ›  Reza

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

  •  0
  • Reza  · 技术社区  · 7 年前

    看起来是这样的:

        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
    

    问题是,可能会有同名的组,其值完全相同。但是,我怎样才能根据组值和“值”使这个唯一?

    我尝试使用array unique,但问题是嵌套数组的值由于某种原因无法工作。我正在尝试另一种方法来做这件事。

    它应该首先检查两个组是否存在相同的值,如果是真的,则应该检查值数组并检查两个值是否相同。如果一切都一样,它应该删除副本。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Magnesium    7 年前

    尝试过这个,似乎很有效,尽管你可能不想把它改写成更有效的东西:

    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;
        }