PHP array_pad()函数 (PHP array_pad() function)
array_pad() function is used to pad an array to given size with a specified value and returns a new array with a specified value.
array_pad()函数用于将数组填充到具有指定值的给定大小,并返回具有指定值的新数组。
Syntax:
句法:
array_pad(array, size, value);
Here,
这里,
array is an array in which we have to add the elements.
数组是我们必须在其中添加元素的数组。
size is the length/size of the array.
size是数组的长度/大小。
value is the value to be added to pad an array.
value是要填充数组的值。
Examples:
例子:
Input:
$arr = array(10, 20, 30);
Function call:
array_pad($arr, 5, 100);
Output:
Array
(
[0] => 10
[1] => 20
[2] => 30
[3] => 100
[4] => 100
)
PHP code:
PHP代码:
<?php
$arr = array(10, 20, 30);
//padding to 5 elements with value 100
$result = array_pad($arr, 5, 100);
//printing
print_r ($result);
$arr = array("Hello", "Guys");
//padding to 5 elements with value "Bye!"
$result = array_pad($arr, 5, "Bye!");
//printing
print_r ($result);
?>
Output
输出量
Array
(
[0] => 10
[1] => 20
[2] => 30
[3] => 100
[4] => 100
)
Array
(
[0] => Hello
[1] => Guys
[2] => Bye!
[3] => Bye!
[4] => Bye!
)
翻译自: https://www.includehelp.com/php/array_pad-function-with-example.aspx