array_slice

(PHP 4 )

array_slice -- 从数组中取出一段

说明

array array_slice ( array array, int offset [, int length])

array_slice() 返回根据 offsetlength 参数所指定的 array 数组中的一段序列。

如果 offset 为正,则序列将从 array 中的此偏移量开始。如果 offset 为负,则序列将从 array 中距离末端这么远的地方开始。

如果给出了 length 并且为正,则序列中将具有这么多的单元。如果给出了 length 并且为负,则序列将终止在距离数组末端这么远的地方。如果省略,则序列将从 offset 开始一直到 array 的末端。

注意 array_slice() 将忽略键名,并且是根据单元在数组中的实际位置来计算偏移量和长度的。

例子 1. array_slice() 例子

<?php
$input
= array ("a", "b", "c", "d", "e");
$output = array_slice ($input, 2);      // returns "c", "d", and "e"
$output = array_slice ($input, 2, -1);  // returns "c", "d"
$output = array_slice ($input, -2, 1);  // returns "d"
$output = array_slice ($input, 0, 3);   // returns "a", "b", and "c"
?>

参见 array_splice()unset()