PHP 8 reference · Array Functions

PHP array_pad() Function

Pad an array to a given length with a value, adding at the end (positive length) or the start (negative).

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_pad(array $array, int $length, mixed $value): array

Parameters

ParameterDescription
array The input array.
length The new size; negative pads at the start.
value The value to add.

Return value

The padded array.

Examples

Pad at the end or startTry it yourself
<?php
echo implode(",", array_pad([1, 2], 5, 0)), "\n";
echo implode(",", array_pad([1, 2], -5, 0)), "\n";

Output

1,2,0,0,0
0,0,0,1,2
Default missing partsTry it yourself
<?php
[$h, $m, $s] = array_pad(explode(":", "14:30"), 3, "00");
echo "$h:$m:$s\n";

Output

14:30:00

Version history

VersionChange
PHP 4.0Added
PHP 8.3No longer limited to adding 1,048,576 elements at a time.

Was this page helpful?