array_pad

Returns a copy of an array padded with a value to a given length, on the right or the left.

Signature
array_pad(array $array, int $length, mixed $value): array
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$arrayarrayThe array to pad. It is not modified.
$lengthintTarget size. A positive value pads on the right, a negative value pads on the left.
$valuemixedThe value inserted into each padded slot.

Return value

arrayA copy of the array padded to the absolute value of length with the given value. If the array is already that long or longer, an unchanged copy is returned.

Errors

Throws a TypeError if array is not an array or length is not an int.

Changelog

  • PHP 8.3.0Only 1048576 elements could be added in one call before this version; the limit is now the maximum array size.

String keys of the original are preserved; only integer keys are discarded and reindexed from 0. So padding ['a' => 1, 7 => 2] keeps the 'a' key but renumbers 7, and the padding values are always appended (or prepended) under fresh integer keys. If you need padding with different values per slot, or want to fill from a starting index, use array_fill() or array_fill_keys() and merge instead.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Ensure every CSV row has the same number of columns before writing
  • Fill out the final row of a grid layout with empty cells
  • Guarantee a split string yields enough parts to destructure safely
  • Left-pad a list of digits with zeros to a fixed width

Frequently asked questions

How do I pad an array on the left in PHP?

Pass a negative length: array_pad($a, -5, 0) makes the array five elements long by prepending zeros. The absolute value is the target size, and the sign only chooses the side.

Does array_pad modify the original array?

No. It returns a padded copy and leaves the input array exactly as it was, so you must assign the result.

Why did array_pad not shorten my array?

It only ever adds elements. If the array is already at or beyond the requested length, you get an unchanged copy back — use array_slice() to truncate.

More examples

Squaring off CSV rows

Every line comes out with exactly three fields: 'Ada,eng,36', then 'Bo,,' and 'Cy,ops,'. Without the pad, the short rows would produce lines with the wrong number of commas and break the parser reading them back.

Example · php
<?php
$rows = [['Ada', 'eng', '36'], ['Bo'], ['Cy', 'ops']];
foreach ($rows as $row) {
    echo implode(',', array_pad($row, 3, '')), "\n";
}

Safe destructuring of a split string

The first input has no colon, so explode returns one element and array_pad supplies the default port. The second already has both parts and the pad does nothing. This avoids an undefined-key warning on the destructuring.

Example · php
<?php
$input = 'localhost';
[$host, $port] = array_pad(explode(':', $input, 2), 2, '3306');
echo "$host on port $port\n";

[$host, $port] = array_pad(explode(':', 'db.example:5432', 2), 2, '3306');
echo "$host on port $port\n";

Integer keys are renumbered, string keys survive

The string keys 'theme' and 'lang' are kept, so the result is ['theme' => 'dark', 'lang' => 'en', 0 => null, 1 => null] — the two padding slots get fresh integer keys. Integer keys are the ones that do not survive: they are always reindexed from 0.

Example · php
<?php
$prefs = ['theme' => 'dark', 'lang' => 'en'];
print_r(array_pad($prefs, 4, null));

Discussion

  • Be the first to comment on this lesson.