ZetCode

PHP array_pad Function

last modified March 13, 2025

The PHP array_pad function pads an array to a specified length with a value. It's useful for ensuring arrays meet size requirements.

Basic Definition

The array_pad function expands an array to a given size by adding elements. If the size is positive, padding occurs at the end; negative at the start.

Syntax: array_pad(array $array, int $length, mixed $value): array. The function returns a new array with the specified length and padding value.

Basic array_pad Example

This demonstrates padding an array to a larger size with a default value.

basic_array_pad.php
<?php

$numbers = [1, 2, 3];
$padded = array_pad($numbers, 5, 0);

print_r($padded);

This pads the array to length 5 with zeros. The original elements remain at the start, with new zeros added at the end.

Negative Length Padding

Using a negative length pads the array at the beginning instead of the end.

negative_padding.php
<?php

$colors = ['red', 'green'];
$padded = array_pad($colors, -4, 'blue');

print_r($padded);

The array is padded to length 4 with 'blue' values added at the start. The original elements shift right in the new array.

Padding with Different Types

The padding value can be of any type, including strings, objects, or arrays.

mixed_type_padding.php
<?php

$data = ['apple', 'banana'];
$padded = array_pad($data, 4, ['fruit' => 'unknown']);

print_r($padded);

This pads the array with associative arrays. Each new element is a complete array with the 'fruit' key set to 'unknown'.

No Padding Needed

When the array is already the requested length, no padding occurs.

no_padding_needed.php
<?php

$values = [10, 20, 30];
$result = array_pad($values, 3, 99);

print_r($result);

The array remains unchanged as its length matches the requested size. The padding value is ignored in this case.

Padding to Smaller Size

If the requested length is smaller than the array, no elements are removed.

smaller_size.php
<?php

$items = ['a', 'b', 'c', 'd'];
$result = array_pad($items, 2, 'x');

print_r($result);

The function never truncates arrays. The original array is returned intact when the requested length is smaller than the current size.

Best Practices

Source

PHP array_pad Documentation

This tutorial covered the PHP array_pad function with practical examples showing its usage for array manipulation scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP Array Functions.