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