百科狗-知识改变命运!
--

next() - 将数组中的内部指针向前移动一位 - php 数组函数

梵高12个月前 (11-21)阅读数 14#技术干货
文章标签数组

next()

(PHP 4, PHP 5, PHP 7)

next() - 将数组中的内部指针向前移动一位 - php 数组函数

将数组中的内部指针向前移动一位

说明

next(array &$array): mixed

next()和current()的行为类似,只有一点区别,在返回值之前将内部指针向前移动一位。这意味着它返回的是下一个数组单元的值并将数组指针向前移动了一位。

参数

$array

受影响的array。

返回值

返回数组内部指针指向的下一个单元的值,或当没有更多单元时返回FALSE

Warning

此函数可能返回布尔值FALSE,但也可能返回等同于FALSE的非布尔值。请阅读布尔类型章节以获取更多信息。应使用===运算符来测试此函数的返回值。

范例

Example #1next()及相关函数的用法示例

注释

Note:你将无法区别包含数组尾以及booleanFALSE单元的数组。要正确遍历可能含有空单元或者单元值为 0 的数组,参见each()函数。

参见

  • current() 返回数组中的当前单元
  • end() 将数组的内部指针指向最后一个单元
  • prev() 将数组的内部指针倒回一位
  • reset() 将数组的内部指针指向第一个单元
  • each() 返回数组中当前的键/值对并将数组指针向前移动一步
Now from PHP 7.2, the function "each" is deprecated, so the has_next I've posted is no longer a good idea. There is another to keep it simple and fast: 
Don't confuse next with continue!
If you're a Perl developer starting with PHP, you might try to use "next" inside a loop to skip to the next iteration...  
i.e., 
foreach ($things as $thing) {
 if (something I don't like about $thing) {
  next;
 }
 blah....
}
The php compiler will take next... but it's not going to work.
Do this instead:
foreach ($things as $thing) {
 if (something I don't like about $thing) {
  continue;
 }
 blah....
}
Papipo's function below is usefull in concept but does not work. 
"Since you do not pass the array by reference, its pointer is only moved inside the function."
This is true, but the array you are manipulating in your has_next() function will have it's pointer set to the first element, not the same position as the original array. What you want to do is pass the array to the has_next() function via reference. While in the has_next() function, make a copy of the array to work on. Find out the current pointer position of the original array and set the pointer on the working copy of the array to the same element. Then you may test to see if the array has a "next" element.
Try the followig insetad: 
I need to know if an array has more items, but without moving array's internail pointer. Thats is, a has_next() function:

Since you do not pass the array by reference, its pointer is only moved inside the function.
Hope that helps.
Take care when replacing code using reset()/next() with code using foreach as foreach does not update the array's internal pointer. This means you cannot, say, use next() to skip an element in foreach loop, or use current() within a function to get a reference to the current element. You probably have code depending on this internal pointer and replacing it will be more work than you anticipated.
See http://www.php.net/foreach
This code returns neighbors of the specified key. The result will be empty if it doesn't have any neighbors. My approach was to use the order of keys to determine neighbors, which is differnet from just getting the next/previous element in an array. Feel free to point out stupidities :) 
This function will return the previous,next neighbors of an array entry within an associative array. If the specified $key points to the last or first element of the array, the first or last keys of the array will be returned consecutively. This is an improved version of the same function posted earlier. 
regarding references with foreach, you can use them directly. Obviating various posts which provide many lines of 'work arounds'.
$array = array(1,2,3,4,5);
foreach($array as &$value)
or use $key
foreach($array as $key => $value)
{
  $array[$key] = '...';
}
This function returns next element in array after your key or false if it last or key doesn't exists in array.

OR 
This function will return the previous,next neighbors of an array entry within an associative array. If the specified $key points to the last or first element of the array, the first or last keys of the array will be returned consecutively. This is an improved version of the same function posted earlier.

Array
(
  [0] => Array
    (
      [RELATED] => Array
        (
          [1] => 4
          [2] => 1
        )
    )
  [1] => Array
    (
      [RELATED] => Array
        (
          [1] => 0
          [2] => 2
        )
    )
  [2] => Array
    (
      [RELATED] => Array
        (
          [1] => 1
          [2] => 3
        )
    )
  [3] => Array
    (
      [RELATED] => Array
        (
          [1] => 2
          [2] => 4
        )
    )
  [4] => Array
    (
      [RELATED] => Array
        (
          [1] => 3
          [2] => 0
        )
    )
)
PHP: 5.2.10-2ubuntu6.3 (default apt-get installation on actual, see Date, jaunty 9.10 Ubuntu Distro - G33kWoRDs)
Have a look at your array pointer if you copy an array - the pointer will be copied, too.
For example if you got this construct:

But if you copy the array after you've setted the pointer, the pointer will be copied, too:

What's more is, that foreach not resetting the pointer after walk through:

The php-functions seems to reset the pointer on the given position after walk through (i don't know the internal handling - there could be used a copy of the array, too):

There are a lot Methods like array_merge($array) that will neither reset the pointer of $array nor copy the pointer to $array2. Have a look on this.
I Hope this was a little helpfull.
This class implements simple operations with array

usage example: 
After dealing with the fact that next() will either returns FALSE when there is no further element in the array or if the element itself is FALSE, I finally find a way to do a has_next() method, which will never fails. You can ignore and/or downvote my last comment, this it will be deleted.
Here is the code that work : 
this may be handy and i didnt know where else to post it.. i need a simple function to cycle through an array i eventually made it into a class so i could have multiple cycles.. if you like it or find it usefull please email me and let me know
class Cycle
{
  var $position;
  var $dataArray;
  var $dataArrayCount;
  
  function Cycle()
  {
    $this->dataArray = func_get_args();
    $this->dataArrayCount = count($this->dataArray);
  }
  
  function Display()
  {
    $this->position = (!isset($this->position) || $this->position >= ($this->dataArrayCount - 1)) ? 0 : $this->position += 1;
    return $this->dataArray[$this->position];
  }
  
}
$bgColor = new Cycle('#000000', '#FFFFFF', '#FF0000');
echo $bgcolor->Display();
//returns #000000
echo $bgcolor->Display();
//returns #FFFFFF
echo $bgcolor->Display();
//returns #FF0000
echo $bgcolor->Display();
//returns #000000
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)