Examples of using foreach to traverse arrays in php arrays (Figure)

Source: Internet
Author: User
Tags array definition php foreach
When using foreach to traverse arrays, errors often occur due to unclear concepts. The following describes common foreach operations. What is foreach traversing an array?

When using foreach to traverse arrays, errors often occur due to unclear concepts. The following describes common foreach operations.

(PHP 4, PHP 5, PHP 7)

The foreach syntax structure provides a simple way to traverse arrays. Foreach can only be applied to arrays and objects. if you try to apply it to variables of other data types or uninitialized variables, an error message will be sent.

Foreach traversal array syntax:

foreach (array_expression as $value)    statementforeach (array_expression as $key => $value)    statement

The first format traverses the given array_expression array.In each loop, the value of the current unit is assigned to $ value and the pointer inside the array moves forward (so the next unit will be obtained in the next loop ).

The second format does the same thing,Only the key name of the current unit is assigned to the variable $ key in each loop.

Objects may be traversed from PHP 5.

Note: When foreach starts to execute, the pointer inside the array automatically points to the first unit. This means that you do not need to call reset () before the foreach loop ().

Note: Unless the array is referenced, foreach operates on a copy of the specified array, rather than the array itself. Foreach has some side effects on array pointers. Do not depend on the value of the array pointer in a foreach loop or after a loop unless it is reset.

Since PHP 5, you can easily add & before $ value to modify the elements of the array. This method will assign values by reference instead of copying a value.

 

$ Value reference is only available when the retrieved array can be referenced (for example, a variable ). The following code cannot be run:

 

Note: The $ value reference of the last element of the array is retained after the foreach loop. We recommend that you use unset () to destroy it. Foreach does not support "@" to suppress error messages.

The following is an example of php foreach traversing arrays.

1. the array to be traversed is a one-dimensional array, which is relatively simple. If it is a single array, it is expressed in two ways, as follows:

 ";} Echo"
"; Foreach ($ array as $ key => $ value) {echo $ key." -- ". $ value ."
";}?>

Output result:

2. if it is a one-dimensional correlated array, it is shown as follows. Note that the key values are different:

 "PHP 文"", "website 2" => "Baidu", "website 3" => "Sogou", "website" => "www.php1.cn "); foreach ($ array as $ value) {echo $ value."
";} Echo"
"; Foreach ($ array as $ key => $ value) {echo $ key." -- ". $ value ."
";}?>

The traversal result is:

3. when traversing a two-dimensional array, it is a little troublesome. at this time, the value is an array, which can derive a variety of methods to operate the array. Next, traverse the basic two-dimensional array:

 ";}?>

The traversal result is as follows:

The second method is used. when traversing an array, pay attention to the key value and do not mix it with the array id value;

 $ Value) {echo "key =". $ key ."
"; Print_r ($ value); echo"
";}?>

The traversal result is as follows:

In addition, it is often necessary to change a column of a two-dimensional array to a one-dimensional array. in this case, the field corresponding to the value should be taken out and placed in a one-dimensional array. if you use a for loop operation, it is complicated, using foreach is relatively simple. obtain the id column and name column of the current array and store them in one-dimensional arrays respectively:

 "1", "name" => "PHP 文""), array ("id" => "2", "name" => "www.php1.cn "), array ("id" => "3", "name" => "Baidu"), array ("id" => "4 ", "name" => "Sogou"); // Two-dimensional array definition // Obtain a column as a one-dimensional array $ idArr = array (); // id column $ nameARR = array (); // name column foreach ($ array as $ key => $ value) {$ arr = $ value ["id"]; $ idArr = $ arr; // store the ID column $ name = $ value ["name"]; $ nameARR = $ name; // store the name column print_r ($ idArr ); print_r ($ nameARR) ;}?>

The traversal result is:

[Recommended tutorials]

1. related topic recommendations: php Array (Array)

2. related video course recommendations: using foreach loop to traverse: index and associated array

The above is the detailed description of the use instance of the foreach traversal array in the php array (figure). For more information, see other related articles in the first PHP community!

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.