Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicate items from an array [duplicate]

I use the line of code below to loop through a table in my database:

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

And if I print the array out:

print_r($items_thread); 

I will get this:

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]         )  ) 

But I want to get rid of the duplicate items in the array, so I use 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]         )  ) 

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

like image 509
Run Avatar asked Feb 18 '11 00:02

Run


People also ask

How do you remove duplicate values from an array?

We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.

How do you remove duplicates from a matrix?

Method : Using loop This task can be performed in brute force manner using loops. In this, we just iterate the list of list using loop and check for the already presence of element, and append in case it's new element, and construct a non-duplicate matrix.


2 Answers

The array_unique function will do this for you. You just needed to add the SORT_REGULAR flag:

$items_thread = array_unique($items_thread, SORT_REGULAR); 

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

like image 102
Tim Cooper Avatar answered Oct 05 '22 19:10

Tim Cooper


You'd be much better off filtering out the duplicates in the SQL query. add a constraint which fetches a UNIQUE recipientID

like image 41
bren Avatar answered Oct 05 '22 20:10

bren