up vote 2 down vote favorite
share [g+] share [fb]

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

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?

Thanks.

link|improve this question

1  
array_unique() is reducing everything to a single array because it's comparing your inner arrays as strings, all of which evaluate to Array. So every array is considered the same. Additionally, from the manual: "Note that array_unique() is not intended to work on multi dimensional arrays." – BoltClock Feb 18 '11 at 0:30
thank you for your suggestion - SELECT DISTINCT RecipientID. It does not return the result I need, even though I can get the unique value from it... you can have a look on the post i made earlier on here - stackoverflow.com/questions/5032645/… thanks. – lauthiamkok Feb 18 '11 at 0:46
possible duplicate of How to remove duplicated 2-dimension array in PHP? – Alix Axel Feb 18 '11 at 1:02
feedback

4 Answers

up vote 1 down vote accepted

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.

link|improve this answer
this is fantastic! thank you :-) – lauthiamkok Feb 18 '11 at 0:34
+1, didn't knew about this. – Alix Axel Feb 18 '11 at 1:55
feedback

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

link|improve this answer
SELECT DISTINCT RecipientID ... – BoltClock Feb 18 '11 at 0:31
feedback

You can use the regular php arrays to achieve this.

$newArray = array();
foreach ($origArray as $user)
{
   $newArray[$user['RecipientID']] = $user;
}
link|improve this answer
feedback

Try this:

$data = array_map('unserialize', array_unique(array_map('serialize', $data)));

Outputs the following:

Array
(
    [0] => 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 also think you should implement this in your database. Also, check my other answer and solutions.

link|improve this answer
thank you for this! what about $items_thread = array_unique($items_thread, SORT_REGULAR); which returns the same result as your code? – lauthiamkok Feb 18 '11 at 1:07
@lauthiamkok: I've never done that, does it really return the same output? If yes, great! – Alix Axel Feb 18 '11 at 1:53
@lauthiamkok: I've read the manual and that flag seems to deliver the correct results, however bare in mind that it's only available in PHP >= 5.2.9. – Alix Axel Feb 18 '11 at 1:55
thanks. I'm on php 5.3.x and my live will be on 5.3.x too so I think I am safe! thank you very much! – lauthiamkok Feb 18 '11 at 2:47
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.