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
Syntax
array_pad(array $array, int $length, mixed $value): array
Parameters
| Parameter | Description |
|---|---|
array |
The input array. |
length |
The new size; negative pads at the start. |
value |
The value to add. |
Return value
The padded array.
Examples
<?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
<?php
[$h, $m, $s] = array_pad(explode(":", "14:30"), 3, "00");
echo "$h:$m:$s\n";
Output
14:30:00
Version history
Related
array_fill()· Create an array filled with the same value, given a start index and a count.str_pad()· Pad a string to a given length with spaces or another string, on the right, left or both sides.- PHP Arrays · tutorial
- PHP Array Functions · tutorial