PHP array_pad function

Last Updated: February 14, 2022

array_pad – pad the array

Syntax

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

Supports (PHP 4, PHP 5, PHP 7, PHP 8)

the array is padded with a specified length and value

length +ve will pad to the right

length -ve will pad to left

Parameters

Parameter Description
array Required. target array for padding
length

length of the padding

value

value to pad

Return Value

returns the copy of the array after padding is done

Example

<?php
$input = array(12, 10, 9);

$result = array_pad($input, 5, 0);
// result is array(12, 10, 9, 0, 0)

?>

Output

Array ( [0] => 12 [1] => 10 [2] => 9 [3] => 0 [4] => 0 ) 
Test