Returns 5 elements and inserts the "blue" value into the new element of the array:
<?php$a=array("red","green");print_r(array_pad($a,5,"blue"));?>The array_pad() function inserts a specified number of elements with a specified value into an array.
Tip: If you set the size parameter to a negative number, the function inserts the new element before the original array (see example below).
Note: If the size parameter is less than the length of the original array, this function will not delete any elements.
array_pad( array,size,value )
| parameter | describe |
|---|---|
| array | Required. Specifies an array. |
| size | Required. Specifies the number of array elements returned from the function. |
| value | Required. Specifies the value of the new element in the array returned from the function. |
| Return value: | Returns an array with new elements. |
|---|---|
| PHP version: | 4+ |
Use a negative size parameter:
<?php$a=array("red","green");print_r(array_pad($a,-5,"blue"));?>