Using pointers for function operations in PHP

Source: Internet
Author: User

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

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.