当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > array_pad() 函数

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 array_pad() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 487 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The array_pad() function inserts a specified number of elements, with a specified value, to an array.
array_pad()函数向指定的数组中插入指定数量的项,并给每个项附上指定的值。

Syntax
语法

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
必要参数。指定从函数中返回的数组元素


Tips and Notes
提示和注意

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参数,那么函数将不会删除任何元素。


Example 1
案例1

<?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 )


Example 2
案例2

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

登陆 | 还没注册?