本文介绍了PHP:删除数组中的重复项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用code线以下遍历一个表在我的数据库,

I use the line of code below to loop through a table in my db,

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

如果我打印出数组,

And if I print the array out,

print_r($items_thread);

我会得到这个,

Array
(
    [0] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => [email protected]
        )

    [1] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => [email protected]
        )

    [2] => Array
        (
            [RecipientID] => 1
            [RecipientScreenname] => Lau T
            [RecipientFirstname] => TK
            [RecipientEmail] => [email protected]
        )

)

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

print_r(array_unique($items_thread));

我得到以下是不太我正在寻找,

I get the weird result below which is not quite I am looking for,

Array
(
    [0] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => [email protected]
        )

)

在理想情况下,我认为它应该返回这一点,

Ideally, I think it should return this,

Array
(
    [0] => Array
        (
            [RecipientID] => 3
            [RecipientScreenname] => Tom L
            [RecipientFirstname] => Thomas
            [RecipientEmail] => [email protected]
        )

    [1] => Array
        (
            [RecipientID] => 1
            [RecipientScreenname] => Lau T
            [RecipientFirstname] => TK
            [RecipientEmail] => [email protected]
        )

)

我该怎么办得到它的权利?我已经使用了错误的PHP语法/默认功能?

What shall I do to get it right? Have I used the wrong php syntax/ default function?

感谢。

推荐答案

array_unique 功能会为你做到这一点。你只需要添加 SORT_REGULAR 标记:

$items_thread = array_unique($items_thread, SORT_REGULAR);

然而,如bren建议,如果可能的话,你应该这样做在SQL中。

However, as bren suggests, you should do this in SQL if possible.

这篇关于PHP:删除数组中的重复项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:18