PHP array traversal foreach syntax structure and example

Source: Internet
Author: User
The following small series will bring you a PHP array traversal foreach syntax structure and example. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at foreach ()

The PHP foreach () syntax structure is used to traverse operations or output arrays. foreach () can only be used to traverse arrays or objects, an error occurs when you try to use it for another data type or an uninitialized variable.

Syntax:

Foreach (array as $ value) statement // Or: foreach (array as $ key => $ value) statement

In the preceding syntax, each cycle assigns the value of the current unit to $ value and the pointer inside the array moves one step forward. In the second syntax format, the key name of the current unit is also assigned to the variable $ key in each loop.

Example:

 18, "li"=>20, "zhang"=>25);foreach ($arr_age as $age) {  echo $age,'
';}?>

Run the sample output:

182025

Use the array key value

 18, "li"=>20, "zhang"=>25);foreach ($arr_age as $key=>$age) {  echo $key,': ',$age,'
';}?>

Running example output:

wang: 18li: 20zhang: 25

When foreach starts to execute, the pointer inside the array automatically points to the first unit, which means that the reset () call is not required before the foreach loop ().

Foreach operates on a copy of the specified array, rather than the array itself. The modification to the returned array unit does not affect the original array (see the example below). However, when the foreach loop ends, the internal pointer of the original array points to the end of the array.

 18, "li" => 20, "zhang" => 25); foreach ($ arr_age as $ age) {$ age = $ age + 10; echo $ age ,'
';} // Output the original array print_r ($ arr_age);?>

Running example output:

283035Array ( [wang] => 18 [li] => 20 [zhang] => 25 )

To modify the original array element in foreach, you can reference it and change the preceding example:

 18, "li" => 20, "zhang" => 25); foreach ($ arr_age as & $ age) {$ age = $ age + 10; echo $ age ,'
';} // Output the original array print_r ($ arr_age);?>

Running example output:

182025 Array ( [wang] => 28 [li] => 30 [zhang] => 35 )

Traverse multi-dimensional arrays

The foreach syntax structure can only be used to traverse one-dimensional arrays. to traverse multi-dimensional arrays, you can use foreach nesting recursively or split the original array into one-dimensional arrays for foreach traversal.

An example of a two-dimensional array mixing:

 18, "li" => 20, "zhang" => array ("name" => "Xiao zhang", "age" => 25 )); foreach ($ arr_age as $ age) {if (is_array ($ age) {foreach ($ age as $ detail) {echo $ detail ,'
';}} Else {echo $ age ,'
';}}?>

The most suitable processing method is to traverse multi-dimensional arrays based on the actual data structure.

PHP arrays are implemented through hashtables. Therefore, foreach traverses arrays based on the order in which elements are added. If you want to traverse by index size, you should use for () loop traversal.

For () loop traversal array

If you are operating on an array of continuous key values, you can also use the for () loop to traverse the array:

 ";}?>

You can also use list () and each () to traverse the array, but the test shows the efficiency.

Not as good as foreach ().

The above PHP array traverses the foreach syntax structure and example is all the content shared by the editor. I hope to give you a reference and support for PHP Chinese.

For more PHP array traversal foreach syntax structure and instance-related articles, please follow the PHP Chinese network!

Related Article

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.