PHP gets an array of key and Value methods summary _php Tutorial

Source: Internet
Author: User
Tags pear

PHP get array Key and value method summary


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:

1

2

3

4

5

6

7

$fruits = Array ("Apple" = "red", "banana" = "yellow");

while ($key = key ($fruits)) {

printf ("%s
", $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:

1

2

3

4

5

6

7

$fruits = Array ("Apple" = "red", "banana" = "yellow");

while ($fruit = current ($fruits)) {

printf ("%s
", $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 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.

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 list () to iterate through the 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 (list ($key, $val) = each ($fruits))

{

echo "$key and $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.

http://www.bkjia.com/PHPjc/1015876.html www.bkjia.com true http://www.bkjia.com/PHPjc/1015876.html techarticle PHP Gets an array of keys and values method summary the process of using an array is often repeated in the group. It is usually necessary to iterate through the array and get the keys or values (or both keys and values), so no ...

  • Related Article

    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.