Remove duplicate items from an array [duplicate]
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.
You'd be much better off filtering out the duplicates in the SQL query. add a constraint which fetches a UNIQUE recipientID
To remove duplicate values then we can use array_unique()
function. The functionality of it is to accept an array and returns another array without duplicate values.
General description of array_unique() is given below:
General Format = array array_unique(array $array [, int $sort_flags=sort_string] )
Parameters = array: Input array ->sort_flags:
The second optional parameter is used to compare items as follows
1. SORT_REGULAR - Normal
2. SORT_NUMERIC - Numerically
3. SORT_STRING - As
4. string SORT_LOCALE_STRING - As string, based on the current locale.
Return Value = Another array without duplicate values
Example 1:
<?php
$array=array('cricket'=>11,'football'=>11,'chess'=>2);
echo"<br/><b>Initially the values of \$array is:</b><br/>";
var_dump($array);
echo"<br/><b>After removing the duplicates:</b><br/>";
print_r(array_unique($array));
?>
Output:
Initially the values of $array is:
array(3) {
["cricket"] => int(11)
["football"] => int(11)
["chess"] => int(2)
}
After removing the duplicates:
Array
(
[cricket] => 11
[chess] => 2
)