Example of using the array pointer function in PHP

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.