The Ultimate Guide to PHP's array_pad Function
In the world of PHP programming, arrays play a crucial role in data storage and manipulation. The array_pad function is one of the many built-in functions that
Arrays are central to almost every PHP program, and sometimes you need an array to be a guaranteed length — padded out with placeholder values when it falls short. PHP's array_pad function does exactly that: it returns a copy of an array grown to a target size, with a value of your choice filling the new slots. This guide covers its syntax, how the sign of the size controls padding direction, the behavior that trips people up, and runnable examples.
What the array_pad Function Does
array_pad returns a new array padded to a length you specify. Two rules govern its behavior:
- If the requested size is larger than the input, copies of your pad value are added — at the end when the size is positive, at the beginning when it is negative.
- If the requested size is less than or equal to the input's current length, the array is returned unchanged.
array_padnever removes elements.
The original array is not modified; you work with the returned value.
Syntax of the array_pad Function
array_pad(array $array, int $length, mixed $value): array$array: The input array you want to pad. It is not modified.$length: The target size. A positive value pads on the right (end); a negative value pads on the left (beginning). The number of elements added isabs($length) - count($array).$value: The value used for every padded element. It can be any type, including another array.
Use Cases for the array_pad Function
The array_pad function is handy when you want to:
- Guarantee an array has at least N elements before iterating over fixed positions.
- Right-align or left-align data by padding the opposite side.
- Fill out a fixed-size grid, form, or table row with default placeholders.
Examples of the array_pad Function
Here are a few examples of how the array_pad function can be used in real-world scenarios:
Example 1: Padding to the End (Positive Size)
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => 0
)The input array $numbers has 3 elements and the target size is 5, so 5 - 3 = 2 zeros are appended to the end.
Example 2: Padding to the Beginning (Negative Size)
Output:
Array
(
[0] => black
[1] => black
[2] => red
[3] => green
[4] => blue
)The size -5 is negative, so padding goes to the beginning. The original 3 colors keep their order and abs(-5) - 3 = 2 copies of 'black' are prepended. Note that the keys are re-indexed from 0 regardless of direction.
Example 3: Filling an Array with a Specific Value
Output:
Array
(
[0] => a
[1] => b
[2] => c
[3] => z
[4] => z
[5] => z
[6] => z
)The input has 3 elements and the target size is 7, so 7 - 3 = 4 copies of 'z' are appended.
Gotchas and Edge Cases
A few behaviors catch people off guard:
- It never shrinks an array. If
abs($length)is less than or equal to the current count, the array comes back unchanged. To remove elements, usearray_sliceorarray_spliceinstead. - String keys are kept, but padding is always numeric. When you pad an associative array, the existing string keys are preserved and the new elements get sequential integer keys:
<?php
$scores = array('math' => 90, 'science' => 85);
print_r(array_pad($scores, 4, 0));
?>Array
(
[math] => 90
[science] => 85
[0] => 0
[1] => 0
)- The pad value can be any type. Passing an array as
$valueinserts copies of that whole array, which is useful for building grids of default rows. - Padding is fixed, not interpolated. Every new slot gets the exact same
$value. If you need a generated or sequential fill, build it witharray_fillorrange().
Related Functions
array_fill— create a new array filled entirely with one value.array_merge— combine arrays instead of padding one.array_sliceandarray_splice— extract or remove portions of an array.array_chunk— split a padded array into fixed-size groups.
Conclusion
array_pad is the quickest way to guarantee an array is a given length without writing a loop. Remember the two rules that drive its behavior: a positive size pads the end, a negative size pads the beginning, and a size at or below the current length leaves the array untouched. With those in mind — plus the key-handling and edge cases above — you can reach for array_pad whenever you need predictable, fixed-size arrays in your PHP projects.