This article mainly introduces how to use array pointer functions in PHP to operate arrays. This article describes current (), key (), next (), prev (), end (), reset () and other array pointer functions. if you need them, refer to the internal pointer of the array, which is the internal structure of the array and points to an element in the array. By default, any element in the array can be accessed by moving or changing the pointer to the first element in the array. PHP provides the following built-in functions for controlling array pointers.
★Current (): obtain the content of the current pointer position.
★Key (): Read the index value (key value) of the data pointed to by the current pointer ).
★Next (): move the internal pointer in the array to the next unit.
★Prev (): returns the internal pointer of an array to one position.
★End (): points the internal pointer of the array to the last element.
★Reset (): Moves the current pointer to the first index position unconditionally.
There is only one parameter for these functions, that is, the array to be operated. In the following example, these array pointer functions are used to control the reading sequence of elements in the array. The code is as follows:
The code is as follows:
<? Php
$ Contact = array (
"ID" => 1,
"Name" => "gao ",
"Company" => "Company ",
"Address" => "Beijing ",
"Telephone" => "(010) 98765432 ",
"EMAIL" => "gao@brophp.com ",
);
// When the array is just declared, the array pointer is at the first element position in the array
Echo 'first element: '. key ($ contact).' => '. current ($ contact ).'
'; // The first element
Echo 'first element: '. key ($ contact).' => '. current ($ contact ).'
'; // The array pointer is not dynamic.
Next ($ contact );
Next ($ contact );
Echo 'third element: '. key ($ contact).' => '. current ($ contact ).'
'; // The third element
End ($ contact );
Echo 'Last element: '. key ($ contact).' => '. current ($ contact ).'
';
Prev ($ contact );
Echo 'second to last element: '. key ($ contact).' => '. current ($ contact ).'
';
Reset ($ contact );
Echo 'returns to the first element:'. key ($ contact). '=>'. current ($ contact ).'
';
?>
In the preceding example, you can use the pointer control functions next (), prev (), end (), and reset () to randomly move pointer positions in the array, and then use key () and current () function to obtain the keys and values of the current position in the array.