PHP array_unique() Function

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

Description

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

Syntax

array_unique(array)

Examples

Example #1

<?php
$input = array("Apple", "Banana", "Banana", "Cherry", "Mango", "Mango", "Orange");
$result = array_unique($input);
print_r($result);
?>

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:

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

Leave A Reply

Your email address will not be published.