English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP array_unique() Function Usage and Example

PHP Array Function Manual

The PHP array_unique() function is used to remove duplicate values from an array

Syntax

array_unique($array);

Definition and Usage

The array_unique() function removes duplicate values from an array. Note that the key names are retained unchanged. array_unique() first sorts the values as strings, then retains the first encountered key name for each value, and then ignores all subsequent key names. This does not mean that the first occurrence of the same value in the unsorted array will retain the key name.

Parameter

Serial NumberParameters and Description
1

array1(Required)

It specifies an array.

Return Value

Accepts an array as input and returns a new array without duplicate values.

Online Example

Use array_unique to remove duplicate element values from a specified array

<?php
   $input = array("a" => "green", "red", "b" => "green", "blue", "red");
   $result = array_unique($input);
   
   print_r($result);
?>
Test and See‹/›

Output Result:

Array ( [a] => green [0] => red [1] => blue )

 PHP Array Function Manual