Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. array_unique() / array_reverse() / array_pad()

array_unique() / array_reverse() / array_pad()

Since: PHP 4(2000)

Functions for removing duplicates from an array, reversing element order, or padding it to a specified length.

Syntax

array_unique(array, sort_flags);

// Reverses the order of elements in an array
array_reverse(array, preserve_keys);

// Pads an array to the specified length with a value
array_pad(array, size, value);

array_fill(start_index, count, value);

Function List

FunctionDescription
array_unique($array, $flags)Removes duplicate values from an array and returns the result. The key of the first occurrence is preserved.
array_reverse($array, $preserve_keys)Returns a new array with the elements in reverse order.
array_pad($array, $size, $value)Pads an array to the specified length with a value and returns the result. A positive size pads the end; a negative size pads the beginning.
array_fill($start_index, $count, $value)Creates and returns an array filled with the specified value.

Sample Code

sample_array_unique.php
<?php
// Remove duplicate Eva pilots
$pilots = ['Ikari Shinji', 'Ayanami Rei', 'Soryu Asuka Langley', 'Ikari Shinji', 'Nagisa Kaworu', 'Ayanami Rei'];
$unique = array_unique($pilots);
print_r($unique); // duplicates removed, leaving 4 elements

// Keys may be non-consecutive — re-index with array_values
$clean = array_values($unique);
print_r($clean); // keys are now sequential starting from 0

// Reverse the order of an array
$ranking = ['Ikari Shinji', 'Ayanami Rei', 'Soryu Asuka Langley'];
$reversed = array_reverse($ranking);
print_r($reversed);

// Pad an array to a specified length
$scores = [80, 90, 70];
$padded_right = array_pad($scores, 5, 0);
print_r($padded_right); // 0s added at the end, 5 elements total

$padded_left = array_pad($scores, -5, 0);
print_r($padded_left); // 0s added at the beginning, 5 elements total

$zeros = array_fill(0, 5, 0);
print_r($zeros);
array_unique.php
php array_unique.php
Array
(
    [0] => Ikari Shinji
    [1] => Ayanami Rei
    [2] => Soryu Asuka Langley
    [4] => Nagisa Kaworu
)
Array
(
    [0] => Ikari Shinji
    [1] => Ayanami Rei
    [2] => Soryu Asuka Langley
    [3] => Nagisa Kaworu
)
Array
(
    [0] => Soryu Asuka Langley
    [1] => Ayanami Rei
    [2] => Ikari Shinji
)
Array
(
    [0] => 80
    [1] => 90
    [2] => 70
    [3] => 0
    [4] => 0
)
Array
(
    [0] => 0
    [1] => 0
    [2] => 80
    [3] => 90
    [4] => 70
)
Array
(
    [0] => 0
    [1] => 0
    [2] => 0
    [3] => 0
    [4] => 0
)

Notes

array_unique() removes duplicate values from an array, but the original keys are preserved, which can result in non-consecutive keys. If you need sequential keys, combine it with array_values(). By default, values are compared as strings; passing SORT_REGULAR as the second argument compares them without type conversion.

array_reverse() returns a new array with the elements in reverse order. Passing true as the second argument preserves the original keys. You can also use it to reverse the order of a sorted array.

array_pad() pads from the beginning if the size is negative, or from the end if it is positive. If the array is already at least as large as the specified size, nothing changes. array_fill() is a convenient way to create an array pre-filled with a uniform initial value.

For sorting arrays, see sort() / rsort().

Keys become non-consecutive

array_unique() preserves the original keys, so after removing duplicates the keys may be non-consecutive. Use array_values() to re-index them if sequential keys are needed.

<?php
$pilots = ['Ikari Shinji', 'Ayanami Rei', 'Ikari Shinji', 'Katsuragi Misato'];
$unique = array_unique($pilots);
print_r($unique); // keys: 0, 1, 3 (2 is missing)

// Re-index with array_values() when sequential keys are required
$clean = array_values($unique);
print_r($clean); // keys: 0, 1, 2
unique_keys.php
php unique_keys.php
Array
(
    [0] => Ikari Shinji
    [1] => Ayanami Rei
    [3] => Katsuragi Misato
)
Array
(
    [0] => Ikari Shinji
    [1] => Ayanami Rei
    [2] => Katsuragi Misato
)

Type-coercing comparisons

By default, values are compared as strings, so 1 and "1" are treated as equal and one is removed. Using SORT_REGULAR makes the comparison type-aware.

<?php
$data = [1, "1", true, 1.0];

// Default (SORT_STRING): compared as strings
$default = array_unique($data);
print_r($default); // only 1 element remains

// SORT_REGULAR: type-aware comparison
$regular = array_unique($data, SORT_REGULAR);
print_r($regular); // may retain more elements
unique_types.php
php unique_types.php
Array
(
    [0] => 1
)
Array
(
    [0] => 1
)

When working with arrays that mix types, verify the result matches your intent beforehand.

Cannot be used directly on multidimensional arrays

Using array_unique() on a multidimensional array triggers a notice and does not work as expected. A different approach is needed to remove duplicate rows.

Problematic pattern (triggers a notice):

<?php
$characters = [
    ['name' => 'Ikari Shinji', 'unit' => 'Unit-01'],
    ['name' => 'Ayanami Rei', 'unit' => 'Unit-00'],
    ['name' => 'Ikari Shinji', 'unit' => 'Unit-01'], // duplicate
];

// Using array_unique directly on a multidimensional array causes a notice
$result = array_unique($characters); // Notice: Array to string conversion
print_r($result);
unique_multi_ng.php
php unique_multi_ng.php
Notice: Array to string conversion

Using json_encode/decode (working approach):

<?php
$characters = [
    ['name' => 'Ikari Shinji', 'unit' => 'Unit-01'],
    ['name' => 'Ayanami Rei', 'unit' => 'Unit-00'],
    ['name' => 'Ikari Shinji', 'unit' => 'Unit-01'], // duplicate
];

$unique = array_values(array_unique(array_map('json_encode', $characters)));
$unique = array_map('json_decode', $unique);
print_r($unique);
unique_multi_ok.php
php unique_multi_ok.php
Array
(
    [0] => stdClass Object
        (
            [name] => Ikari Shinji
            [unit] => Unit-01
        )
    [1] => stdClass Object
        (
            [name] => Ayanami Rei
            [unit] => Unit-00
        )
)

Practical Patterns

Deduplicating tags

Removing duplicate character attribute tags and formatting them as a list.

<?php
$tags_raw = ['Pilot', 'NERV', 'Pilot', 'Angel', 'NERV', 'Nagisa Kaworu'];
$tags = array_values(array_unique($tags_raw));
echo implode(', ', $tags);
unique_tags.php
php unique_tags.php
Pilot, NERV, Angel, Nagisa Kaworu

Setting defaults with array_pad

Initializing deployment flags for 5 pilots and setting some to active.

<?php
$active = [1, 1]; // Ikari Shinji and Ayanami Rei are deployed
$flags = array_pad($active, 5, 0); // fill 5 slots with default 0
print_r($flags);
unique_pad.php
php unique_pad.php
Array
(
    [0] => 1
    [1] => 1
    [2] => 0
    [3] => 0
    [4] => 0
)

If you find any errors or copyright issues, please .