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

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_splice() 函数


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

Definition and Usage
定义和用法

The array_splice() function removes selected elements from an array and replaces it with new elements. The function also returns an array with the removed elements.
array_splice()函数的作用是:从数组中删除指定的元素,并用新的元素替代它们;同时,函数将以数组的形式返回这些被删除的元素。

Syntax
语法

array_splice(array,start,length,array)

Parameter参数 Description描述
array Required. Specifies an array
必要参数。指定一个数组。
start Required. Numeric value. Specifies where the function will start removing elements. 0 = the first element. If this value is set to a negative number, the function will start that far from the last element. -2 means start at the second last element of the array.
必要参数。数值。指定需要返回的元素在数组中的位置。0表示第一个元素。如果设定的值是一个负数,那么表示返回的起始元素从数组的最后位置开始算起。-2表示数组中的倒数第二个元素。
length Optional. Numeric value. Specifies how many elements will be removed, and also length of the returned array. If this value is set to a negative number, the function will stop that far from the last element. If this value is not set, the function will remove all elements, starting from the position set by the start-parameter.
可选参数。数值型。指定需要返回的数组长度(即:以起始元素为基准从前往后数[长度数量包括起始元素])。如果这个值为负数,那么返回的长度是从起始元素开始往前返回的数量(即:以起始元素为基准从后往前数[长度数量包括起始元素]);如果这个指没有被设置,那么函数将从start参数设置的起始位置开始返回所有参数
array Optional. Specifies an array with the elements that will be insertet to the original array. If it's only one element, it can be a string, and does not have to be an array.
可选参数。指定需要插入原始数组的数组元素。如果这个需要插入另一数组中的数组只包含一个元素,它也可以是一个元素,并非一定是一个数组。


Tips and Notes
提示和注意点

Tip: If the function does not removes any elements (length=0), the replacment array will be inserted from the position of the start parameter. (See example 3)
提示:如果函数并未指定需要删除的部分(length=0),那么用于替代的数组将从start参数设置的位置插入数组中。

Note: The keys in the replacement array are not preserved.
注意:用于替代的数组中,键并不保留。


Example 1
案例1

<?php
$a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
$a2=array(0=>"Tiger",1=>"Lion");
array_splice($a1,0,2,$a2);
print_r($a1);
?>

The output of the code above will be:
上述代码将输出下面的结果:

Array ( [0] => Tiger [1] => Lion [2] => Horse [3] => Bird ) 


Example 2
案例2

The same example as example 1, but the output is the returned array:
案例2的例子与案例1雷同,只不过结果是以数组的形式返回的,具体如下:

<?php
$a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
$a2=array(0=>"Tiger",1=>"Lion");
print_r(array_splice($a1,0,2,$a2));
?>

The output of the code above will be:
上述代码将输出下面的结果:

Array ( [0] => Dog [1] => Cat )


Example 3
案例3

With the length parameter set to 0:
案例3将length参数设置为0

<?php
$a1=array(0=>"Dog",1=>"Cat");
$a2=array(0=>"Tiger",1=>"Lion");
array_splice($a1,1,0,$a2);
print_r($a1);
?>

The output of the code above will be:
上述代码将输出下面的结果:

Array ( [0] => Dog [1] => Tiger [2] => Lion [3] => Cat )

评论 (0) All

登陆 | 还没注册?