PHP: array_pad() function
PHP: Pad array to the specified length with a value
The array_pad() function is used to padding (insert) a specified number of elements, with a specified value, into an array.
If the specified value is positive then the array is padded on the right, if it is negative then padded on the left.
If the specified value is less than or equal to the length of the input then no padding takes place.
Version
(PHP 4 and above)
Syntax
array array_pad (input_array, pad_size, pad_value)
Parameters
Name | Description | Required / Optional |
Type |
---|---|---|---|
input_array | Specifies the name of the array. | Required | Array |
pad_size | A total number of elements in the resulting array after padding. | Required | Array |
pad_value | Value with which padding will take place, must be less than the size of the input_array. | Required | Mixed* |
*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.
Return value
An array with new elements.
Value Type : Array
Example:
<?php
$input_array= array(1,7,10,6);
$result=array_pad($input_array,5,330);
print_r($result);
?>
Output:
Array ( [0] => 1 [1] => 7 [2] => 10 [3] => 6 [4] => 330 )
Pictorial Presentation:

View the example in the browser
Practice here online :
See also
Previous: array_multisort
Next: array_pop
PHP: Tips of the Day
Filters the collection using the given callback
Example:
<?php function tips_reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); } print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) { return strlen($item) > 4; })); ?>
Output:
Array ( [0] => Pear [1] => Kiwi )
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises