array_pad()

Published Jan 8, 2024
Contribute to Docs

The array_pad() method pads an array to a specified length with a value. If the specified length is positive, the array is padded to the right with the value until the array’s size equals the specified length. Conversely, if the specified length is negative, the array is padded to the left with the value until the array’s size equals the absolute value of the specified length. The array will not be padded if the size of the array is more than the absolute value of the specified length.

Syntax

array_pad($array, $length, $value)
  • $array: Specifies the input array.
  • $length: The new size of array.
  • $value: The value to be padded to array if its size is less than the absolute value of $length.

Note: The array_pad() function returns the padded array according to the specified length with the intended value.

Example

The following example uses the array_pad() function to pad the array $numbers to different lengths and values:

<?php
$numbers = array(1, 2, 3);
$paddedArray = array_pad($numbers, 5, "hello");
$paddedArray2 = array_pad($numbers, -5, "you");
$paddedArray3 = array_pad($numbers, 2, "nope");
print_r($paddedArray);
print_r($paddedArray2);
print_r($paddedArray3);
?>

The example above will result in the following output:

Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => hello
[4] => hello
)
Array
(
[0] => you
[1] => you
[2] => 1
[3] => 2
[4] => 3
)
Array
(
[0] => 1
[1] => 2
[2] => 3
)

Codebyte Example

This example is runnable and uses the array_pad() function in various scenarios:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn PHP on Codecademy