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

时间:2018-08-12 21:43:56

标签: php arrays sorting unique

我有一个嵌套的多维数组-每个数组都有一个名为values的嵌套数组,其中包含一个数组

它看起来像这样:

    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

问题在于,可能会有具有完全相同值的相同命名组。但是如何根据组值和“值”使它唯一?

我尝试使用唯一的数组,但是问题是嵌套数组的值由于某种原因而无法使用。我正在尝试找到另一种方法来做到这一点。

它应该首先检查两个组是否存在相同的值,如果是,则应该检查值数组并检查两个值是否相同。如果全部相同,则应删除重复项。

1 个答案:

答案 0 :(得分:0)

试过这个,似乎可行,尽管您可能不想将其重写为更有效的内容:

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