Home > Article > Backend Development > php-Arrays function-array_pad-fill the array to the specified length with values_PHP tutorial
array_pad() pads the array to the specified length with values
【Function】
This function will return a copy of the specified array, padded with the specified value to the specified length.
If the specified length is positive, the array is filled to the right, if it is negative, it is filled from the left.
If the absolute value of the specified length is less than or equal to the length of the original array, there will be no padding
It is possible to fill up to 1048576 array elements at a time
【Scope of use】
php4, php5.
【Use】
array array_pad( array input,int pad_size,mixed pad_value )
input/required/original array
pad_size/required/specified length to be padded
pad_value/required/is the specified value
【Example】
[php]
$input=array(12,10,9);
var_dump( array_pad($input,5,0) );
var_dump( array_pad($input,-7,-1) );
var_dump( array_pad($input,2,"noop") );
/*
array(5) {
[0]=>
int(12)
[1]=>
int(10)
[2]=>
int(9)
[3]=>
int(0)
[4]=>
int(0)
}
array(7) {
[0]=>
int(-1)
[1]=>
int(-1)
[2]=>
int(-1)
[3]=>
int(-1)
[4]=>
int(12)
[5]=>
int(10)
[6]=>
int(9)
}
array(3) {
[0]=>
int(12)
[1]=>
int(10)
[2]=>
int(9)
}
*/
Excerpted from zuodefeng’s notes