Summary of PHP methods for getting the keys and values of arrays, and php methods for getting the array Summary
This article describes how PHP obtains the keys and values of arrays. Share it with you for your reference. The details are as follows:
When using arrays, you often need to traverse arrays. It is usually necessary to traverse the array and obtain each key or value (or simultaneously obtain the key and value), so it is not surprising that PHP provides some functions to meet the needs. Many functions can complete two tasks, not only to obtain the key or value of the current pointer position, but also to move the pointer to the next appropriate position.
Get the current array key ()
The key () function returns the key where the current pointer is located in input_array. The format is as follows:
Mixed key (array)
The following example outputs the key of the $ fruits array by iteratively processing the array and moving the pointer:
$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 every time key () is called. Therefore, the next () function must be used to complete the task of advancing the pointer.
Get the current array value current ()
The current () function returns the array value of the current pointer position in the array. The format is as follows:
Mixed current (array)
Next, modify the previous example to obtain the array value:
$fruits = array("apple"=>"red", "banana"=>"yellow");while ($fruit = current($fruits)) { printf("%s <br />", $fruit); next($fruits);}// red // yellow
Get the current array key and value each ()
The each () function returns the current key/value pair of input_array and pushes the pointer to a position. The format is as follows:
Array each (array)
The returned array contains four keys. Keys 0 and key contain the key name, while keys 1 and value contain the corresponding data. If the pointer before execution of each () is located at the end of the array, false is returned.
$fruits = array("apple", "banana", "orange", "pear");print_r ( each($fruits) );// Array ( [1] => apple [value] => apple [0] => 0 [key] => 0 )
Each () is often used in combination with list () to traverse arrays. This example is similar to the previous example, but the entire array is output cyclically:
$fruits = array("apple", "banana", "orange", "pear");reset($fruits);while (list($key, $val) = each($fruits)){ echo "$key => $val<br />";}// 0 => apple// 1 => banana// 2 => orange// 3 => pear
Because the original array pointer is reset when an array is assigned to another array, if $ fruits is assigned to another variable in the previous example, an infinite loop will occur.
This completes the array traversal.
I hope this article will help you with jQuery programming.