如何在PHP中从二维数组中删除重复数组[关闭]

kx5bkwkv  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(111)

已关闭,此问题需要更focused。它目前不接受回答。
**想改善这个问题吗?**更新问题,使其只关注editing this post的一个问题。

23小时前关闭
Improve this question

[0] => Array
    (
        [event_date] => 2023-10-15
        [sale_id] => 11959
        [product_name] => 10x10 Commercial Pop-Up Tent | IMPACT Poly Top
        [product_id] => 14
        [qty_stock] => 3
        [qty_booked] => 10
        [qty_deficit] => -7
        [eventDate] => 2023-10-13
        [pickupDate] => 2023-10-16
    )

[1] => Array
    (
        [event_date] => 2023-10-15
        [sale_id] => 11959
        [product_name] => 10x10 Commercial Pop-Up Tent | IMPACT Poly Top
        [product_id] => 14
        [qty_stock] => 3
        [qty_booked] => 10
        [qty_deficit] => -7
        [eventDate] => 2023-10-15
        [pickupDate] => 2023-10-15
    )

我只需要一个数组元素。

[0] => Array
        (
            [event_date] => 2023-10-15
            [sale_id] => 11959
            [product_name] => 10x10 Commercial Pop-Up Tent | IMPACT Poly Top
            [product_id] => 14
            [qty_stock] => 3
            [qty_booked] => 10
            [qty_deficit] => -7
            [eventDate] => 2023-10-13
            [pickupDate] => 2023-10-16
        )
x0fgdtte

x0fgdtte1#

一个选项可以是使用自定义过滤器函数,如:

function array_unique_multidimensional(array $array): array {
        
        $sums = [];
        $return_array = [];
        
        foreach($array as $element) {
            $sum = md5(join('', $element));
            if(!in_array($sum, $sums)) {
                $sums[] = $sum;
                $return_array[] = $element;
            }
        }
        
        return $return_array;
    }

这当然要求数组元素中的键总是以相同的顺序出现,并且你希望根据all键进行唯一过滤。

相关问题