ZetCode

PHP array_unique Function

last modified March 13, 2025

The PHP array_unique function removes duplicate values from an array. It's useful for cleaning data and ensuring unique elements.

Basic Definition

The array_unique function takes an array and returns a new array without duplicate values. It preserves the keys of the first occurrence.

Syntax: array_unique(array $array, int $flags = SORT_STRING): array. The flags parameter controls comparison behavior (SORT_REGULAR, SORT_NUMERIC).

Basic array_unique Example

This shows the simplest usage of removing duplicate values from an array.

basic_array_unique.php
<?php

$numbers = [1, 2, 2, 3, 4, 4, 5];
$uniqueNumbers = array_unique($numbers);

print_r($uniqueNumbers);

Output: Array ( [0] => 1 [1] => 2 [3] => 3 [4] => 4 [6] => 5 ). The function removed duplicates while keeping first occurrences and keys.

String Array Example

Remove duplicate strings from an array while preserving case sensitivity.

string_array.php
<?php

$fruits = ["apple", "Apple", "banana", "banana", "orange"];
$uniqueFruits = array_unique($fruits);

print_r($uniqueFruits);

Output: Array ( [0] => apple [1] => Apple [2] => banana [4] => orange ). Note "apple" and "Apple" are treated as different due to case sensitivity.

Using Flags Parameter

Demonstrate how the flags parameter affects comparison behavior.

flags_parameter.php
<?php

$values = ["10", 10, "20", 20, "30"];
$uniqueRegular = array_unique($values, SORT_REGULAR);
$uniqueString = array_unique($values, SORT_STRING);

echo "SORT_REGULAR: ";
print_r($uniqueRegular);

echo "SORT_STRING: ";
print_r($uniqueString);

SORT_REGULAR treats "10" and 10 as equal (type juggling), while SORT_STRING treats them as different. Choose the appropriate flag for your needs.

Associative Array Example

Remove duplicates from an associative array while preserving keys.

associative_array.php
<?php

$users = [
    "john" => "admin",
    "jane" => "editor",
    "bob" => "admin",
    "alice" => "viewer"
];

$uniqueRoles = array_unique($users);

print_r($uniqueRoles);

Output: Array ( [john] => admin [jane] => editor [alice] => viewer ). The duplicate "admin" role was removed while keeping the first occurrence.

Multidimensional Array Challenge

Show limitations with multidimensional arrays and a workaround solution.

multidimensional_array.php
<?php

$data = [
    ["id" => 1, "name" => "John"],
    ["id" => 2, "name" => "Jane"],
    ["id" => 1, "name" => "John"],
];

// array_unique won't work directly on multidimensional arrays
$serialized = array_map('serialize', $data);
$unique = array_unique($serialized);
$result = array_map('unserialize', $unique);

print_r($result);

This workaround serializes each element for comparison, then unserializes back. Output shows only unique objects based on all properties.

Best Practices

Source

PHP array_unique Documentation

This tutorial covered the PHP array_unique function with practical examples showing its usage for removing duplicate array values.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP Array Functions.