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
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/php/function-reference/array_pad.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics