当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > array_pad() 函数
The array_pad() function inserts a specified number of elements, with a specified value, to an array.
array_pad()函数向指定的数组中插入指定数量的项,并给每个项附上指定的值。
array_pad(array,size,value) |
| Parameter参数 | Description描述 |
|---|---|
| array | Required. Specifies an array 必要参数。指定一个数组 |
| size | Required. Specifies the number of elements in the array returned from the function 必要参数。指定从函数中返回的数组元素数量(即:对这些元素的数量进行限定) |
| value | Required. Specifies the value of the new elements in the array returned from the function 必要参数。指定从函数中返回的数组元素 |
Tip: If you assign a negative size parameter, the function will insert new elements BEFORE the original elements. (See example 2)
提示:如果你给size参数指定一个负数,这个函数将在原始元素之前插入新元素。(见案例2)
Note: This function will not delete any elements if the size parameter is less than the original array's size.
注意:如果size参数小于原始数组的size参数,那么函数将不会删除任何元素。
<?php
$a=array("Dog","Cat");
print_r(array_pad($a,5,0));
?> |
The output of the code above will be:
上述代码将产生下面的结果:
Array ( [0] => Dog [1] => Cat [2] => 0 [3] => 0 [4] => 0 ) |
With a negative size parameter:
Size参数为负数:
<?php
$a=array("Dog","Cat");
print_r(array_pad($a,-5,0));
?> |
The output of the code above will be:
上述代码将输出下面的结果:
Array ( [0] => 0 [1] => 0 [2] => 0 [3] => Dog [4] => Cat ) |
评论 (0) All