PHP array_pad()Functions
Examples
Return 5 elements, and insert the value "blue" into the new elements of the array:
<?php
$a=array("red","green");
print_r(array_pad($a,5,"blue"));
?>
$a=array("red","green");
print_r(array_pad($a,5,"blue"));
?>
Run example »
Definition and Usage
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 will insert new elements before the original array (see the example below).
Note:If the size parameter is less than the length of the original array, the function will not delete any elements.
Syntax
array_pad(array,size,value)
| Parameter | 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 elements in the array returned by the function. |
Technical Details
| Return value: | Returns an array with the new elements. |
|---|---|
| PHP Version: | 4+ |
More Examples
Example 1
Using a negative value for the size parameter:
<?php
$a=array("red","green");
print_r(array_pad($a,-5,"blue"));
?>
$a=array("red","green");
print_r(array_pad($a,-5,"blue"));
?>
Run example »
Complete PHP Array Reference Other Extensions