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.
<?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.
<?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.
<?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.
<?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.
<?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
- Type Consistency: Use the same type for padding as array elements.
- Performance: Avoid padding very large arrays unnecessarily.
- Readability: Clearly document padding values and purposes.
- Edge Cases: Handle cases where padding might not occur.
Source
This tutorial covered the PHP array_pad
function with practical
examples showing its usage for array manipulation scenarios.
Author
List all PHP Array Functions.