The function inserts a specified number of new elements with the specified value at the beginning of the end of the array.
You will understand the functioning after gaining through the syntax. However, a negative size parameter inserts new elements in the staring index of the array (before the original elements).
If the size parameter is less than the size of the array, no element will be discarded from the output.
What is the syntax of the array_oad function in PHP?
array_pad(array, size, value)
Parameters | Details |
---|---|
array | Specify the array to pad – Required |
size | Number of elements to return from the array – Required |
value | Specify the value to the newly inserted elements in the array – Required |
Examples of array_pad function
<?php
$arr=array("pink","blue");
print_r(array_pad($arr,4,"green"));
?>
In the above example, we inserted an additional color to the array and return 4 elements of the array.
<?php
$arr=array("pink","blue");
print_r(array_pad($arr,-5,"green"));
?>
In the above example, we inserted an additional element at the beginning of the
array.