PHP gets an array of key and Value methods Summary, PHP get array summary _php tutorial

Source: Internet
Author: User
Tags pear

PHP gets an array of key and Value methods Summary, PHP get array summary


The example in this paper describes how PHP gets the keys and values of an array. Share to everyone for your reference. Specific as follows:

In the process of using arrays, you often need to go through the group. It's not surprising that PHP provides some functions to meet the requirements, usually by iterating through the array and getting the keys or values (or getting the keys and values at the same time). Many functions can accomplish two tasks, not only to get the key or value of the current pointer position, but also to move the pointer down an appropriate position.

Gets the current array key key ()

The key () function returns the keys at the position of the current pointer in Input_array. The form is as follows:

Mixed key (array array)

The following example outputs the keys of the $fruits array by iterating over the array and moving the pointer:

$fruits = Array ("Apple" = "red", "banana" = "yellow"), while ($key = key ($fruits)) {   
", $key); Next ($fruits);} Apple//Banana

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

Gets the current array value of the present ()

The current () function returns the array value in the array where the pointer is currently located. The form is as follows:

Mixed current (array array)

The following changes the previous example, this time we want to get the array value:

$fruits = Array ("Apple" = "red", "banana" = "yellow"), while ($fruit = current ($fruits)) {   
", $fruit);

Gets the current array key and value each ()

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

Array each (array array)

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

$fruits = Array ("Apple", "banana", "orange", "pear");p rint_r (each ($fruits));//Array ([1] = apple [value] = AP ple [0] = 0 [key] = 0)

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

$fruits = Array ("Apple", "banana", "orange", "pear"), reset ($fruits), while (List ($key, $val) = each ($fruits)) {   echo] $key = $val
";} 0 = apple//1 = banana//2 = orange//3 = Pear

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

This completes the traversal of the array.

It is hoped that this article will be helpful to everyone's jquery program design.

http://www.bkjia.com/PHPjc/1016332.html www.bkjia.com true http://www.bkjia.com/PHPjc/1016332.html techarticle PHP gets an array of key and Value methods Summary, PHP get array Summary This article describes the PHP get array key and value method. Share to everyone for your reference. As follows: Using arrays ...

  • 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.