PHP array_unique() Function

The array_unique is used to removes duplicate values from an array.


The array_unique() function removes duplicate values from an array. This function take an input array and returns a new array without duplicate values. If two or more array values are the same, the first appearance will be kept and the other will be removed.

Read Also : Remove Last Element From An Array In PHP




Example #1

$input = array("Apple", "Banana", "Banana", "Cherry", "Mango", "Mango", "Orange");
$result = array_unique($input);

In the above example i have “Banana”, “Mango” values are repeated two times in an array but when i use array_unique() function then it will return only unique value as below output:

    [0] => Apple
    [1] => Banana
    [3] => Cherry
    [4] => Mango
    [6] => Orange

Leave A Reply

Your email address will not be published.