Php traversal array list foreach each 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, 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:

The Code is as follows: Copy code

<? Php
$ Color = array ('White '=> 'White ',
'Black' => 'black ',
'Red' => 'red ',
'Green' => 'green ',
'Yellow' => 'yellow ');
Foreach ($ color as $ c) echo $ c. "<br> ";
?>


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: Copy code


Foreach ($ color as $ c) echo $ c. "<br> ";

Changed:

Foreach ($ color as $ key => $ c) echo $ key. $ c. "<br> ";


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:

The Code is as follows: Copy code

<? Php
$ 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]. "<br> ";
$ 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:

The Code is as follows: Copy code
<? Php
$ Date = array (1 => "Monday ",
2 => "Tuesday ",
3 => "Wednesday ");
List ($ key, $ value) = each ($ date);/* traversal function */
Echo "$ key $ value". "<br>";/* 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: Copy code

<? Php
$ 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]. "<br>";/* 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.