array_pad
Fill a value into an array with a specified length
array_pad()
function inserts a specified number of elements with the specified value into the array.
Tip: If you set the size parameter to a negative number, the function inserts a new element before the original array (see example below).
Note: If the size parameter is less than the length of the original array, the function will not delete any elements.
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" ) ) ; ?>
Try it yourself
Use negative size parameter:
<?php $a = array ( "red" , "green" ) ; print_r ( array_pad ( $a , - 5 , "blue" ) ) ; ?>
Try it yourself
array_pad ( array , size , value )
parameter | describe |
---|---|
array | Required. Specify array. |
size | Required. Specifies the number of elements in the array returned from the function. |
value | Required. Specifies the value of the new element in the array returned from the function. |