Use the following function to remove all duplicate values in an array.
function remove_duplicated_values($array){ foreach($array as $key=>$val){ $newArray[$val] = 1; } }
The way this function works is by looping through the array and assigning each value of the array to be a key of a new array and setting the value as 1. As the values of the array are added to the new array any new values will lengthen the array and any duplicate values will reset to be 1.
The keys of the new array are then returned as an array of values using the array_keys() PHP function.
Here is an example of the function in action.
$array = remove_duplicated_values($array);
Be aware that any keys that the original array has will be lost by the action of this function.
0 comments:
Post a Comment