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
)