PHP array_pad() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_pad() makes an array a certain length by adding a value to its end or its start.

A positive length pads at the end, and a negative length pads at the start. If the array is already long enough, it is returned unchanged.

Syntax

Call array_pad() with the values you need. Start with this line.

PHP
$padded = array_pad($row, 4, "");
CallWhat it does
array_pad([1, 2], 5, 0)Returns [1, 2, 0, 0, 0].
array_pad([1, 2], -5, 0)Returns [0, 0, 0, 1, 2].
array_pad([1, 2, 3], 2, 0)Returns [1, 2, 3] because it is already long enough.

The length is the final size you want, not the number of items to add.

Library declarations
PHP
array_pad(array $array, int $length, mixed $value): array

Parameters

ParameterDescription
arrayRequired. The array to pad.
lengthRequired. The new size. Positive pads at the end, negative pads at the start.
valueRequired. The value used for the new items.

Return Value

Return ValueDescription
arrayA new array padded to the given length, or a copy of the original if it is already long enough.

PHP Examples

Example 1 — Pad at the End

Fill a short list up to five items with zeros.

PHP
<?php
$numbers = [1, 2];

$padded = array_pad($numbers, 5, 0);

echo implode(", ", $padded), PHP_EOL;
echo count($padded), PHP_EOL;

How It Works

  1. Start with two items. The array holds 1 and 2.
  2. Ask for length 5. PHP needs three more items.
  3. Add the zeros. The value 0 is appended three times.
  4. Count the result. The array now has exactly five items.

Example 2 — Pad at the Start and Longer Arrays

Use a negative length to pad at the front. A short length changes nothing.

PHP
<?php
echo implode(", ", array_pad([1, 2], -5, 0)), PHP_EOL;
echo implode(", ", array_pad([1, 2, 3], 2, 0)), PHP_EOL;

How It Works

  1. Use -5. The minus sign means pad at the beginning.
  2. Read the result. Three zeros come before the original items.
  3. Ask for a smaller length. The array already has 3 items, which is more than 2.
  4. Nothing is cut. The array comes back as it was.

Example 3 — Make Every Row the Same Width

Pad a short row so it has four columns, like when reading CSV data.

PHP
<?php
$row = ["Ana", "Pune"];

$row = array_pad($row, 4, "");

echo implode("|", $row), PHP_EOL;
echo count($row), PHP_EOL;

How It Works

  1. Start with a short row. It has two columns.
  2. Pad with an empty string. Two empty cells are added.
  3. Join with a pipe. The empty cells show as nothing between the bars.
  4. Count the columns. Every row now has four.

Frequently Asked Questions

It adds a value to an array until the array reaches the length you ask for. It can pad at the end or the start.
Use a negative length, such as array_pad($array, -5, 0).
The array is returned unchanged. array_pad() never removes items.
No. It returns a new array, so assign the result to a variable.
Yes. PHP limits how many items can be added in one call, so avoid asking for extremely large lengths.

Next: array_pop()

array_pop() removes the last item from an array and returns it.

array_pop() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful