Step Through an Array in PHP

Description

The following code shows how to step Through an Array.

Example


//from www.jav a 2s.c o m
<!DOCTYPE html>
<html>
  <body>
    <h1>Stepping Through an Array</h1>
    <?php
    
    $authors = array( "A", "B", "C", "D" );
    
    echo "<p>The array: " . print_r( $authors, true ) . "</p>";
    
    echo "<p>The current element is: " . current( $authors ) . ".</p>";
    echo "<p>The next element is: " . next( $authors ) . ".</p>";
    echo "<p>...and its index is: " . key( $authors ) . ".</p>";
    echo "<p>The next element is: " . next( $authors ) . ".</p>";
    echo "<p>The previous element is: " . prev( $authors ) . ".</p>";
    echo "<p>The first element is: " . reset( $authors ) . ".</p>";
    echo "<p>The last element is: " . end( $authors ) . ".</p>";
    echo "<p>The previous element is: " . prev( $authors ) . ".</p>";
    
    ?>

  </body>
</html>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone