PHP array traversal foreach syntax structure and instance, foreach syntax _ PHP Tutorial

Source: Internet
Author: User
Tags php foreach
PHP array traversal foreach syntax structure and instance, foreach syntax. PHP array traversal foreach syntax structure and instance. foreach syntax foreach () PHPforeach () syntax structure is used to traverse operations or output arrays. foreach () can only be used to traverse arrays or objects, when the PHP array traverses the foreach syntax structure and instance, the foreach syntax

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:

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

Run the sample output:

182025

Use the array key value

<?php$arr_age = array("wang"=>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.

<? Php $ arr_age = array ("wang" => 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:

<? Php $ arr_age = array ("wang" => 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:

182025Array ( [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:

<? Php $ arr_age = array ("wang" => 18, "li" => 20, "zhang" => array ("name" => "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:

<?php$arr_age = array(18, 20, 25);$num = count($arr_age);for($i = 0; $i < $num; $i++){  echo $arr_age[$i]."
";}?>

You can also use list () and each () to traverse the array, but the test shows that the efficiency is not as high as that of foreach ().

The above PHP array traversal foreach syntax structure and examples are all the content shared by the editor. I hope to give you a reference, and I hope you can also support the help house.

Http://www.bkjia.com/PHPjc/1136648.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1136648.htmlTechArticlePHP array traversal foreach syntax structure and instance, foreach syntax foreach () PHP foreach () syntax structure is used to traverse the operation or output array, foreach () can only be used to traverse the array or object, when...

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.