Php traversal array list method summary

Source: Internet
Author: User
In php, there are many functions that can be used to traverse arrays, such as the for statement, list, each, and foreach functions. these are also the main functions used to traverse arrays in php, I will introduce it to you below

In php, there are many functions that can be used to traverse arrays, such as the for statement, list, each, and foreach functions. these are also the main functions used to traverse arrays in php, next I will introduce it to you.
 
Foreach traversal array
When using arrays, we often need to traverse the array and obtain each key or element value. php provides some functions dedicated to traversing the array. This section describes the usage of foreach's array Traversal function.

Structure:

Foreach (array_expression as $ value) statement
/* Array_expression is the array to be traversed
As is used to assign the value of the array to $ value.
Statement is a subsequent statement.
*/
Instance 1:

$ Color = array ('White '=> 'white ',
'Black' => 'black ',
'Red' => 'red ',
'Green' => 'green ',
'Yellow' => 'yellow ');
Foreach ($ color as $ c) echo $ c ."
";
?>
 
Using foreach, you can obtain not only the element value, but also the key name. The structure is as follows:

Foreach (array_expression as $ key => $ value) statement
Run the following code on the above instance:

The code is as follows:

Foreach ($ color as $ c) echo $ c ."
";

Changed:

Foreach ($ color as $ key => $ c) echo $ key. $ c ."
";
 
Each traversal array
Traversing arrays is an important part of php Array Operations. in addition to the foreach function mentioned above, we will introduce an array Traversal function-each ().

The each () function can be used to output the key name and corresponding element value at the current pointer position. You can use "0" or "key" to access the key name (identifier), and use "1" or "value" to access the value corresponding to the identifier.

Instance:

$ Ages = array (1 => "php ",
5 => "html ",
10 => "css ");
$ A = each ($ ages);/* traverse the array for the first time */
Echo $ a [0]. "t ";
Echo $ a [1]."
";
$ A = each ($ ages);/* The second traversal array */
Echo $ a [key]. "t ";
Echo $ a [value];
?>
 

List traversal array

The function list can be assigned to the variable once when the array is traversed. it is usually used with the each () function. Using the list () function makes it easier to access keys and values returned by each.

Instance:

$ Date = array (1 => "Monday ",
2 => "Tuesday ",
3 => "Wednesday ");
List ($ key, $ value) = each ($ date);/* Traversal function */
Echo "$ key $ value "."
";/* Output the first array */
$ Next = next ($ date);/* move the pointer behind */
Echo "$ next ";
?>


Ps: The list () function is just opposite to the array () function. array () constructs a series of data into an array, while list () splits the array into data.

For traversal array

In addition to some predefined php array traversal functions, we can also use the loop feature of the for statement to output the array traversal. The following is an example:

The code is as follows:

$ A [] = "Zhang Xueyou";/* define an array */
$ A [] = "Andy Lau ";
$ A [] = "Dawn ";
$ A [] = "Guo Fucheng ";
$ S = count ($ a);/* count the number of arrays */
For ($ I = 0; $ I <$ s; $ I ++) {/* traverse the array */
Echo $ a [$ I]."
";/* Display 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.