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

array_shift

(PHP 4 , PHP 5)

array_shift --  将数组开头的单元移出数组

说明

mixed array_shift ( array array)

array_shift()array 的第一个单元移出并作为结果返回,将 array 的长度减一并将所有其它单元向前移动一位。所有的数字键名将改为从零开始计数,文字键名将不变。如果 array 为空(或者不是数组),则返回 NULL

注: 在调用后,该函数会重置(reset())数组的指针。

例子 1. array_shift() 例子

<?php
$stack
= array ("orange", "banana", "apple", "raspberry");
$fruit = array_shift ($stack);
print_r($stack);
?>

这将使 $stack 剩下 3 个单元:

Array
(
   [0] => banana
   [1] => apple
   [2] => raspberry
)

并且 orange 被赋给了 $fruit

参见 array_unshift()array_push()array_pop()




add a note add a note User Contributed Notes
array_shift
archangel at uro dot mine dot nu
04-Mar-2003 06:11
Here's a fun little trick for cycling a set of items in the case where you want to say loop thorugh a set of styles for a tiled effect:

<?php
$styles = array('style1', 'style2', 'style3', ... 'stylen');

while (haveMoreToPrint())
{
  array_push($styles, array_shift($styles));

   print('<div class="'.$styles[0].'">Some content</div>');
}
?>

And since the elements that are being shifted off go onto the end, there is no concern as to how many or how few styles your have or how many times you are going to cycle through them.  No counters, no nothin.

<array_searcharray_slice>
 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