Codectionary / Developer documentation / PHP

Remove Duplicate Values with array_unique

Deduplicate simple values while preserving first occurrences and choosing comparison behaviour deliberately.

What it is and why to use it

Repeated tags can appear when combining several article lists. array_unique() returns an array containing the first occurrence of each equal value. It preserves the key of that first occurrence, so deduplicating a list can leave gaps. Combine it with array_values when the final result should be a sequential list.

How it works

The default comparison flag is SORT_STRING: values with the same string representation compare equal. Strings are case-sensitive under this comparison, so PHP and php remain different tags. The function does not modify the original array. For mixed types or structured records, choose a deliberate identity rule rather than assuming this helper provides strict object or deep-array equality.

Common mistakes and limitations

Do not use array_unique as a replacement for a database unique constraint or as a generic deep deduplicator. Integer 1 and string "1" collapse under SORT_STRING. If business rules ignore case, normalise appropriate text first and decide how to preserve the original display label.

Run the examples locally

Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.

The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.

These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.

Check your interpreter, lint and run one saved example

php -v
php -l example.php
php example.php

Continue learning

Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.

Syntax

$distinct = array_values(array_unique($values, SORT_STRING));

Examples

Create a distinct tag list

Keep the first occurrence of each repeated topic name.

<?php
declare(strict_types=1);

$tags = ['PHP', 'Web', 'PHP', 'Arrays', 'Web'];
$unique = array_values(array_unique($tags, SORT_STRING));
echo json_encode($unique, JSON_THROW_ON_ERROR), PHP_EOL;
  1. array_unique retains PHP at its first position and drops the later equal PHP and Web values.
  2. array_values closes the resulting key gaps so json_encode produces a JSON array.
  3. The first-seen ordering is useful for a tag display assembled from several sources.
  4. Expected output: ["PHP","Web","Arrays"]

Inspect the default equality rule

Compare values whose types and letter case differ.

<?php
declare(strict_types=1);

$values = [1, '1', 'PHP', 'php'];
$unique = array_values(array_unique($values));
echo json_encode($unique, JSON_THROW_ON_ERROR), PHP_EOL;
  1. The first two values both become the string "1" for comparison, so only the first, integer 1, remains.
  2. PHP and php have different string representations and both remain.
  3. The output makes the comparison rule visible. Use strict membership logic instead when integer and string identifiers must be distinct.
  4. Expected output: [1,"PHP","php"]

Best practices

  • Document what makes two values duplicates.
  • Reindex only when the result is conceptually a list.

Runtime & versions

PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

PHP Manual

Specifications & further reading