Current Location: Home> Function Categories> array_pad

array_pad

Fill a value into an array with a specified length
Name:array_pad
Category:Array
Programming Language:php
One-line Description:Fill the array with values ​​to the specified length.

Definition and usage

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.

Example

Example 1

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

Example 2

Use negative size parameter:

 <?php
$a = array ( "red" , "green" ) ;
print_r ( array_pad ( $a , - 5 , "blue" ) ) ;
?>

Try it yourself

grammar

 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.
Similar Functions
Popular Articles