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

// Removes duplicate values from an array.
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);

// Creates an array filled with a specified 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

<?php
// Remove duplicate values.
$tags = ['PHP', 'HTML', 'CSS', 'PHP', 'JavaScript', 'HTML'];
$unique = array_unique($tags);
print_r($unique); // Duplicates are removed, leaving 4 elements.

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

// Reverse the order of an array.
$ranking = ['Gold', 'Silver', 'Bronze'];
$reversed = array_reverse($ranking);
print_r($reversed); // Reversed to: Bronze, Silver, Gold.

// 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, resulting in 5 elements.

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

// Create an array filled with a specified value.
$zeros = array_fill(0, 5, 0);
print_r($zeros); // An array of five 0s is created.

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, but 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().

If you find any errors or copyright issues, please .