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?
array_unique()
is reducing everything to a single array because it's comparing your inner arrays as strings, all of which evaluate toArray
. So every array is considered the same. Additionally, from the manual: "Note thatarray_unique()
is not intended to work on multi dimensional arrays." – Teplitz