search for in the  
<natsortpos>
Last updated: Thu, 19 May 2005

next

(PHP 3, PHP 4, PHP 5)

next --  Advance the internal array pointer of an array

Description

mixed next ( array &array )

Returns the array value in the next place that's pointed to by the internal array pointer, or FALSE if there are no more elements.

next() behaves like current(), with one difference. It advances the internal array pointer one place forward before returning the element value. That means it returns the next array value and advances the internal array pointer by one. If advancing the internal array pointer results in going beyond the end of the element list, next() returns FALSE.

Warning

If the array contains empty elements, or elements that have a key value of 0 then this function will return FALSE for these elements as well. To properly traverse an array which may contain empty elements or elements with key values of 0 see the each() function.

Example 1. Example use of next() and friends

<?php
$transport
= array('foot', 'bike', 'car', 'plane');
$mode = current($transport); // $mode = 'foot';
$mode = next($transport);    // $mode = 'bike';
$mode = next($transport);    // $mode = 'car';
$mode = prev($transport);    // $mode = 'bike';
$mode = end($transport);    // $mode = 'plane';
?>

See also current(), end(), prev(), and reset().



User Contributed Notes
next
brentimus
28-Apr-2005 12:10
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:

<?php
function has_next(&$array)
{
  
$A_work=$array//$A_work is a copy of $array but with its internal pointer set to the first element.
  
$PTR=current($array);
  
array_set_pointer($A_work, $PTR);

   if(
is_array($A_work))
   {
       if(
next($A_work)===false)
           return
false;
       else
           return
true;
   }
   else
       return
false;
}

function
array_set_pointer(&$array, $value)
{
  
reset($array);
   while(
$val=current($array))
   {
       if(
$val==$value)
           break;

      
next($array);
   }
}
?>
papipo's gmail account
13-Oct-2004 03:47
I need to know if an array has more items, but without moving array's internail pointer. Thats is, a has_next() function:

<?php
function has_next($array) {
   if (
is_array($array)) {
       if (
next($array) === false) {
           return
false;
       } else {
           return
true;
       }
   } else {
       return
false;
   }
}

$array = array('fruit', 'melon');
if (
has_next($array)) {
   echo
next($array);
}

// prints 'melon'
?>

Since you do not pass the array by reference, its pointer is only moved inside the function.
Hope that helps.
lukasz at karapuda dot com
18-Aug-2004 12:06
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.

<?php
function array_neighbor($arr, $key)
{
  
$keys = array_keys($arr);
  
$keyIndexes = array_flip($keys);
 
  
$return = array();
   if (isset(
$keys[$keyIndexes[$key]-1])) {
      
$return[] = $keys[$keyIndexes[$key]-1];
   }
   else {
      
$return[] = $keys[sizeof($keys)-1];
   }
  
   if (isset(
$keys[$keyIndexes[$key]+1])) {
      
$return[] = $keys[$keyIndexes[$key]+1];
   }
   else {
      
$return[] = $keys[0];
   }
  
   return
$return;
}
?>
court shrock
20-May-2004 06:36
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 :)

<?php

function array_neighbor($arr, $key)
{
  
krsort($arr);
  
$keys = array_keys($arr);
  
$keyIndexes = array_flip($keys);
  
  
$return = array();
   if (isset(
$keys[$keyIndexes[$key]-1]))
      
$return[] = $keys[$keyIndexes[$key]-1];
   if (isset(
$keys[$keyIndexes[$key]+1]))
      
$return[] = $keys[$keyIndexes[$key]+1];

   return
$return;
}

?>
bm at ANTISPAM dot solidwave dot com
16-Apr-2004 11:49
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

<natsortpos>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT