PHP array_unique() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_unique() removes duplicate values from an array.

When duplicates are found, the first occurrence is kept and the later ones are dropped. Keys are preserved, so you may see gaps in the numbering.

Syntax

Call array_unique() with the values you need. Start with this line.

PHP
$unique = array_unique($colors);
CallWhat it does
array_unique(["a", "b", "a"])Returns an array with "a" and "b".
array_unique([4, "4", 4.0])Treats all three as the same value and keeps the first.
array_values(array_unique($list))Removes duplicates and renumbers the keys.

By default values are compared as strings. The comparison is case-sensitive, so "a" and "A" are different. Wrap the call in array_values() when you need keys 0, 1, 2...

Library declarations
PHP
array_unique(array $array, int $flags = SORT_STRING): array

Parameters

ParameterDescription
arrayRequired. The array to remove duplicates from.
flagsOptional. How to compare values: SORT_STRING (default), SORT_REGULAR, SORT_NUMERIC or SORT_LOCALE_STRING.

Return Value

Return ValueDescription
arrayA new array with duplicates removed. The first occurrence of each value keeps its original key.

PHP Examples

Example 1 — Remove Duplicate Values

Clean up a list that contains repeated colors.

PHP
<?php
$colors = ["red", "blue", "red", "green", "blue"];
$unique = array_unique($colors);

echo implode(", ", $unique), PHP_EOL;
echo "Keys: ", implode(",", array_keys($unique)), PHP_EOL;

How It Works

  1. The list has five items with "red" and "blue" repeated.
  2. array_unique() keeps the first "red" and first "blue" and drops the repeats.
  3. The result has three values: red, blue and green.
  4. The surviving keys are 0, 1 and 3, so the numbering has a gap.

Example 2 — Case and Type Rules

The default comparison is case-sensitive and compares values as strings.

PHP
<?php
$names = ["a", "A", "a"];
echo implode(", ", array_unique($names)), PHP_EOL;

$mixed = [4, "4", 4.0];
echo count(array_unique($mixed)), PHP_EOL;

How It Works

  1. "a" and "A" are different strings, so both survive.
  2. The second "a" is a duplicate of the first and is removed.
  3. The values 4, "4" and 4. 0 all become the string "4" when compared.
  4. Only one of them is kept, so count() gives 1.

Example 3 — Count Unique Words

Split a sentence into words and find the distinct ones.

PHP
<?php
$text = "to be or not to be";
$words = explode(" ", $text);
$unique = array_values(array_unique($words));

echo implode(" ", $unique), PHP_EOL;
echo "Unique words: ", count($unique), PHP_EOL;

How It Works

  1. explode() splits the sentence into six words.
  2. array_unique() removes the repeated "to" and "be".
  3. array_values() renumbers the keys so the list is clean.
  4. The distinct words are printed along with their count.

Frequently Asked Questions

It removes duplicate values from an array and keeps the first occurrence of each value.
array_unique() preserves the original keys. Use array_values() to renumber them from 0.
Yes. "Apple" and "apple" are different. Lowercase the values first with array_map('strtolower', $array) if you want to ignore case.
By default it compares them as strings (SORT_STRING). You can change this with the second argument.
Not directly. You can use SORT_REGULAR for simple cases, or serialize each row, remove duplicates, then unserialize.

Next: array_unshift()

array_unshift() adds one or more values to the beginning of an array.

array_unshift() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful