一尘不染

从数组中删除重复项

php

我使用下面的代码行遍历数据库中的一个表:

$items_thread = $connection -> fetch_all($sql);

如果我打印出数组:

print_r($items_thread);

我会得到这个:

Array
(
    [0] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => info@xx.com
        )

    [1] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => info@xx.com
        )

    [2] => Array
        (
            [RecipientID] => 1
            [RecipientScreenname] => Lau T
            [RecipientFirstname] => TK
            [RecipientEmail] => lau@xx.co.uk
        )

)

但是我想摆脱数组中的重复项,所以我使用 array_unique

print_r(array_unique($items_thread));

我得到下面的奇怪结果,这不是我想要的结果:

Array
(
    [0] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => info@xx.com
        )

)

理想情况下,我认为它应该返回以下内容:

Array
(
    [0] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => info@xx.com
        )

    [1] => Array
        (
            [RecipientID] => 1
            [RecipientScreenname] => Lau T
            [RecipientFirstname] => TK
            [RecipientEmail] => lau@xx.co.uk
        )

)

我该怎么做才能正确处理?我使用了错误的PHP语法/默认功能吗?


阅读 238

收藏
2020-05-29

共1个答案

一尘不染

array_unique功能将为您完成此操作。您只需要添加SORT_REGULAR标志:

$items_thread = array_unique($items_thread, SORT_REGULAR);
2020-05-29