HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

array_pad

Description

The array_pad of Array for PHP pad array to the specified length with a value.

Syntax

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

Parameters

array

Initial array of values to pad.

length

New size of the array.

value

Value to pad if array is less than length.

Return

Returns a copy of the array padded to size specified by length with value value. If length is positive then the array is padded on the right, if it's negative then on the left. If the absolute value of length is less than or equal to the length of the array then no padding takes place.

Examples

1 · length · +

<?

$array = [0, 1];
$length = 5;
$value = "pad";

$return = array_pad($array, $length, $value);

print_r($return);
Array
(
    [0] => 0
    [1] => 1
    [2] => pad
    [3] => pad
    [4] => pad
)

2 · length · -

<?

$array = [0, 1];
$length = -5;
$value = "pad";

$return = array_pad($array, $length, $value);

print_r($return);
Array
(
    [0] => pad
    [1] => pad
    [2] => pad
    [3] => 0
    [4] => 1
)

3 · length · <=

<?

$array = [0, 1];
$length = 2;
$value = "pad";

$return = array_pad($array, $length, $value);

print_r($return);
Array
(
    [0] => 0
    [1] => 1
)