An internal pointer to an array is an organization mechanism inside 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, and you can access any element in the array. For the control of array pointers PHP provides the following built-in functions to take advantage of.
Current (): Gets the contents of the present pointer position.
Key (): reads the index value (key value) of the data pointed to by the current pointer.
Next (): Moves the inner pointer in the array to the next cell.
Prev (): Returns the internal pointer of the array back to one bit.
End (): Points The inner pointer of the array to the last element.
Reset (): Moves the current pointer unconditionally to the first index position.
The parameters of these functions are only one, that is, the array itself to manipulate. 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:
< PHP
$contact = Array (
"ID" = 1,
"Name" = "Gao MoU",
"Company" = "Company A",
"Address" = "Beijing",
"Tel" = "(010) 98765432",
"Email" = "[email protected]",
);
When an array is just declared, the array pointer is positioned in the first element of the array
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 ' second-to-last element: '. Key ($contact). ' = '. Current ($contact). ' <br> ';
Reset ($contact);
Echo ' returned to the first element: '. Key ($contact). Current ($contact). ' <br> ';
?>
In the example above, by using the pointer control function next (), Prev (), End () and reset () arbitrarily move the pointer position in the array, then use the key () and the current () function to get the key and value of the position in the array.
Using pointers for function operations in PHP