Home >Backend Development >PHP Tutorial >php-Arrays function-array_pad-fill the array to the specified length with values_PHP tutorial

php-Arrays function-array_pad-fill the array to the specified length with values_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:51:091261browse

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478218.htmlTechArticlearray_pad() fills the array to the specified length with values ​​[Function] This function will return a copy of the specified array, And pad it to the specified length with the specified value. If the specified length is...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn