An internal pointer to an array is an organizational mechanism within an array that points to an element in an array. The default is to point to the first element in the array by moving or changing the position of the pointer, you can access any element in the array. For control of array pointers PHP provides the following several built-in functions that can be exploited.
★current (): Obtains the current position of the pointer content data.
★key (): reads the index value (the key value) of the data that the current pointer points to.
★next (): Moves the internal pointer in the array to the next cell.
★prev (): Returns an array's internal pointer back to a bit.
★end (): Points the array's internal pointer to the last element.
★reset (): Moves the current pointer unconditionally to the first index position.
The parameters of these functions are only one, which is the array itself to be manipulated. In the following example, these array pointer functions are used to control the reading order of the elements in the array. The code looks like this:
Copy Code code as follows:
<?php
$contact = Array (
"ID" => 1,
"Name" => "Gao MoU",
"Company" => "a Company",
"Address" => "Beijing",
"Telephone" => "(010) 98765432",
"EMAIL" => "gao@brophp.com",
);
The position of the array pointer at the first element in the array when the array is just declared
Echo ' first element: '. Key ($contact). ' => '. Current ($contact). <br> '; First element
Echo ' first element: '. Key ($contact). ' => '. Current ($contact). <br> '; Array pointer not moving
Next ($contact);
Next ($contact);
Echo ' third element: '. Key ($contact). ' => '. Current ($contact). <br> '; A third element
End ($contact);
Echo ' last element: '. Key ($contact). ' => '. Current ($contact). <br> ';
Prev ($contact);
Echo ' penultimate element: '. Key ($contact). ' => '. Current ($contact). <br> ';
Reset ($contact);
Echo is back to the first element: '. Key ($contact). ' => '. Current ($contact). <br> ';
?>
In the previous example, move the pointer position in the array by using the pointer control function next (), Prev (), End (), and Reset (), and then use the key () and current () functions to get the key and value of the position in the array.