Move cursor in array and output in PHP

Description

The following code shows how to move cursor in array and output.

Example


//from www.  j  a  v  a2  s. co m
<?php
   $people = array("XML", "CSS", "HTML", "Javascript");

   echo current($people) . "<br>"; // The current element is XML
   echo next($people) . "<br>"; // The next element of XML is CSS
   echo current($people) . "<br>"; // Now the current element is CSS
   echo prev($people) . "<br>"; // The previous element of CSS is XML
   echo end($people) . "<br>"; // The last element is Javascript
   echo prev($people) . "<br>"; // The previous element of Javascript is HTML
   echo current($people) . "<br>"; // Now the current element is HTML
   echo reset($people) . "<br>"; // Moves the internal pointer to the first element of the array, which is XML
   echo next($people) . "<br>"; // The next element of XML is CSS

   print_r (each($people)); // Returns the key and value of the current element (now CSS), and moves the internal pointer forward
?>

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