Current Location: Home> Function Categories> next

next

Move the inner pointer in the array one by one
Name:next
Category:Array
Programming Language:php
One-line Description:Move the inner pointer in the array one by one.

Definition and usage

The next() function points the internal pointer to the next element in the array and outputs it.

Related methods:

  • prev() - Point the inner pointer to the previous element in the array and output
  • current() - Returns the value of the current element in the array
  • end() - Point the inner pointer to the last element in the array and output
  • reset() - Points the inner pointer to the first element in the array and outputs
  • each() - Returns the key name and key value of the current element and moves the internal pointer forward

Example

Example 1

Output the values ​​of the current element and the next element in the array:

 <?php
$people = array ( "Bill" , "Steve" , "Mark" , "David" ) ;

echo current ( $people ) . "<br>" ;
echo next ( $people ) ;
?>

Try it yourself

Example 2

Demonstrate all related methods:

 <?php
$people = array ( "Bill" , "Steve" , "Mark" , "David" ) ;

echo current ( $people ) . "<br>" ; // The current element is Bill
echo next ( $people ) . "<br>" ; // The next element of Bill is Steve
echo current ( $people ) . "<br>" ; // The current element is Steve now
echo prev ( $people ) . "<br>" ; // The previous element of Steve is Bill
echo end ( $people ) . "<br>" ; // The last element is David
echo prev ( $people ) . "<br>" ; // The element before David is Mark
echo current ( $people ) . "<br>" ; // The current current element is Mark
echo reset ( $people ) . "<br>" ; // Move the internal pointer to the first element of the array, i.e. Bill
echo next ( $people ) . "<br>" ; // The next element of Bill is Steve

print_r ( each ( $people ) ) ; // Returns the key name and key value of the current element (currently Steve) and moves the internal pointer forward
?>

Try it yourself

grammar

 next ( array )
parameter describe
array Required. Specifies the array to be used.

illustrate

next() and current() behave similarly, with only a little difference, moving the inner pointer one by one before returning the value. This means it returns the value of the next array unit and moves the array pointer one by one. If the result of moving the pointer exceeds the end of the array unit, next() returns FALSE.

Note: If the array contains empty cells, or the value of the cell is 0, the function will also return FALSE when encountering these cells. To correctly traverse an array that may contain empty cells or unit values ​​0, see each() function.

Similar Functions
Popular Articles