超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<array_shiftarray_splice>
Last updated: Fri, 22 Jun 2007

array_slice

(PHP 4 , PHP 5)

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




add a note add a note User Contributed Notes
array_slice
dams at php dot net
16-Dec-2001 11:09
Here is a version of Array_slice which takes into account keys.

That may be a suggestion for future developpement.

<?php
function array_slice_key($array, $offset){
 if (!is_array($array))
  return FALSE;
 
if (func_num_args() == 3){
   $length = func_get_arg(2);
  $length = max(0,intval($length));
 } else {
   $length = count($array);
 }
 
 $i = 0;
 $return = array();
$keys = array_slice(array_keys($array), $offset, $length);
 foreach( $keys as $key){
   $return[$key] = $array[$key];
 }
 return $return;
}
?>

<array_shiftarray_splice>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22