Summary of PHP methods for retrieving the keys and values of arrays. PHP obtains the keys and values of an array. when using an array, it is often necessary to traverse the array. Generally, you need to traverse the array and obtain each key or value (or obtain both the key and value). Therefore, you do not need to use PHP to obtain the key and value methods of the array.
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:
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 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:
1 2 3 4 5 6 7 |
$ Fruits = array ("apple" => "red", "banana" => "yellow "); While ($ fruit = current ($ fruits )){ Printf ("% s ", $ 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.
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 combination with list () to traverse arrays. This example is similar to the previous example, but the entire array is output cyclically:
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 => $ val "; } // 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.
When arrays are used by workers, arrays are often traversed. It is usually necessary to traverse the array and obtain each key or value (or simultaneously obtain the key and value), so no...