Return 5 elements, inserting the value “blue” into the new positions in the array:
<?php $a=array(“red”,“green”); print_r(array_pad($a,5,“blue”)); ?> |
The array_pad()
function adds a specified number of elements with a given value to an array.
Tip: If a negative size parameter is used, the function will insert new elements before the original elements (see example below).
Note: This function does not remove any elements if the size parameter is less than the size of the original array.
array_pad(array, size, value) |
Parameter |
Description |
array |
Required. Defines an array. |
size |
Required. Specifies the number of elements in the array returned by the function. |
value |
Required. Specifies the value for the new elements in the array returned by the function. |
Return Value: |
Returns an array with added elements. |
PHP Version: |
4+ |
Using a negative size value:
<?php $a=array(“red”,“green”); print_r(array_pad($a,-5,“blue”)); ?> |