Using array pointer function in PHP to manipulate an array sample _php tips

Source: Internet
Author: User
Tags prev

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.

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.