A summary of the key and value methods for getting arrays in PHP

Source: Internet
Author: User
Tags arrays mixed pear printf

The enumeration group is often used in the process of using arrays. It's not surprising that PHP provides a number of functions to meet the requirements, usually by traversing the array and getting the keys or values (or both). Many functions can accomplish two tasks, not only to get the key or the value of the current pointer position, but also to move the pointer down to an appropriate position.

Gets the current array key ()

The key () function returns the keys in the input_array where the current pointer is located. The form is as follows:

Mixed key (array array)

The following example uses the iteration to process the array and move the pointer to output the key of the $fruits array:

1 2 3 4 5 6 7 $fruits = Array ("Apple" => "red", "banana" => "yellow"); while ($key = key ($fruits)) {printf ("%s <br/>", $key); next ($fruits);}//Apple//Banana

Note that the pointer is not moved each time the key () is invoked. To do this you need to use the next () function, the only function of which is to complete the task of pushing the pointer.

Gets the current array value ()

The current () function returns the value of the array in the array at which the pointer is currently positioned. The form is as follows:

Mixed current (array array)

Let's revise the previous example, this time to get the array value:

1 2 3 4 5 6 7 $fruits = Array ("Apple" => "red", "banana" => "yellow"); while ($fruit = current ($fruits)) {printf ("%s <br/>", $fruit); next ($fruits);}//Red//Yellow

Gets the current array key and value each ()

The each () function returns the current key/value pair of Input_array and pushes the pointer one position. The form is as follows:

Array each (array array)

The returned array contains four keys, the key 0 and key contain the key names, and the key 1 and value contain the corresponding data. Returns False if the previous pointer at the end of the array executes each ().

1 2 3 $fruits = Array ("Apple", "banana", "orange", "pear"); Print_r (each ($fruits)); Array ([1] => Apple [value] => Apple [0] => 0 [key] => 0)

Each () is often used in conjunction with the list () to traverse an array. This example is similar to the previous example, but the loop outputs the entire array:

1 2 3 4 5 6 7 8 9 10 $fruits = Array ("Apple", "banana", "orange", "pear"); Reset ($fruits); while (the list ($key, $val) = each ($fruits)) {echo "$key => $val <br/>";}//0 => Apple//1 => banana//2 = > Orange//3 => Pear

Because assigning an array to another array resets the original array pointer, so in the previous example, if we assign $fruits to another variable within the loop, we will cause an infinite loop.

This completes the traversal of 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.