PHP | array_pad() Function
The array_pad() is a builtin function in PHP and is used to pad a value fixed number of time onto an array. This function inserts an element specified number of times into an array either at front or back.
Syntax:
array array_pad($input_array, $input_size, $values)
Parameters: This function accepts three parameters, all of this which are mandatory to be supplied.
- $input_array (mandatory): Refers to the array, on which the operation is to be performed, or to which the elements are needed to be added.
- $total_size (mandatory): Refers to the total size of the new array to be returned.
- If the value is positive, the elements get added to the end of the array.
- If the value is negative, the elements get added at the beginning of the array.
- $values (mandatory): Refers to the value with which padding will take place. The padding takes place only when $total_size is greater than the size of the input_array.
Return Value: The function returns a copy of the array padded to the size of $total_size. If the absolute value of $total_size is less than or equal to the length of the array then no padding takes place. It is possible to add, at most 1048576 elements at a time.
Examples:
Input : array = ("one", "two", "three", "four", "five")
$total_size = 7 , $value = "six"
Output :
Array
(
[0] => one
[1] => two
[2] => three
[3] => four
[4] => five
[5] => six
[6] => six
)
Input : array = ("one", "two", "three", "four", "five")
$total_size = -7 , $value = "six"
Output :
Array
(
[0] => six
[1] => six
[2] => one
[3] => two
[4] => three
[5] => four
[6] => five
)
Below programs explains the working of the array_pad() function:
-
Padding elements at end of the array when the $total_size is positive:
<?php// PHP function to illustrate the use of array_pad()functionPadding($array,$string){$result=array_pad($array, 7,$string);return($result);}$array=array("one","two","three","four","five");$string="six";print_r(Padding($array,$string));?>chevron_rightfilter_noneOutput:
Array ( [0] => one [1] => two [2] => three [3] => four [4] => five [5] => six [6] => six ) -
Padding elements at start of the array when the $total_size is negative:
<?php// PHP function to illustrate the use of array_pad()functionPadding($array,$string){$result=array_pad($array, -7,$string);return($result);}$array=array("one","two","three","four","five");$string="six";print_r(Padding($array,$string));?>chevron_rightfilter_noneOutput:
Array ( [0] => six [1] => six [2] => one [3] => two [4] => three [5] => four [6] => five )
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- PHP | Ds\Set first() Function
- D3.js | d3.map.has() Function
- D3.js | d3.map.set() Function
- PHP | exp() Function
- p5.js | tan() function
- PHP | Ds\Set last() Function
- p5.js | hue() function
- p5.js | day() function
- PHP | pow( ) Function
- PHP | Ds\Map last() Function
- p5.js | value() Function
- p5.js | pow() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



