Phpforeach loop usage introduction _ PHP Tutorial

Source: Internet
Author: User
Phpforeach loop usage introduction. In php, the foreach statement is used to traverse the array cyclically. The value of the current array element is assigned to the value variable (the array pointer moves down one by one) for example, in php, the foreach statement is used to traverse the array cyclically. The value of the current array element is assigned to the value variable (the array pointer moves down one by one) for each loop)

Example

The code is as follows:

/* Php foreach loop simple example */
$ A = array ('a' => 'web', 'B' => 'friends', 'C' => 'others ',);
// This is a hash array. now we output the content
Foreach ($ a as $ key => $ value ){
Echo $ key. '->'. $ value .'
';
}
?>
$ A = array ('a' => 'web', 'B' => 'friends', 'C' => 'others ',);


Start running foreach for the first time, then $ key = 'a'; $ value = 'net ';
After the output is run for the second time, $ key = 'B'; $ value = 'friends ';
After the output is run for the third time, $ key = 'C'; $ value = 'Others ';
At this time, the array has reached the end and the loop is exited.


Example

The code is as follows:

1, "two" => 2, "three" => 3, "four" => 4 );
Foreach ($ array1 as $ key => $ value)
{Echo "KEY: $ key value: $ value n ";}

$ Array2 = array (5, 6, 7, 8 );
Foreach ($ array2 as & $ value)
{
$ Value = $ value * 2;
Echo "$ valuen ";
}

?>

The result is

$ Array1 = array ("one" => 1, "two" => 2, "three" => 3, "four" => 4 );
Foreach ($ array1 as $ key => $ value)
{Echo "KEY: $ key value: $ value n ";}

$ Array2 = array (5, 6, 7, 8 );
Foreach ($ array2 as & $ value)
{
$ Value = $ value * 2;
Echo "$ valuen ";
}

?>

For ease of understanding, we assume that $ array is a one-dimensional array, $ key is the index of the array, $ value is the value of the index, and their names can be random, $ key and $ value are called for ease of understanding. To help you better understand the foreach process, we create an array:
$ Array = array ('first '=> 'IBM', 'second', 'HP ');
Now we simulate the PHP service to use foreach to traverse $ array:

The code is as follows:
Foreach ($ array as $ key => $ value)
{
Echo $ key. '=>' $ value;
}

In the first loop, $ key = 'first ', $ value = 'IBM'. in this case, the actual foreach executes an invisible operation on $ array.

Note the following when using references in a foreach loop:

I found a problem that is prone to errors, but does not understand the principle, but does not understand the problem.

The code is as follows:
Foreach ($ array as & $ row ){
$ Row = explode ('/', $ row );
}
Foreach ($ array as $ row ){
// Do something
}

In this case, a logic error occurs in the second loop. in the second loop, do something outputs $ row, and the output at the end of the loop is the second-to-last element, instead of the last

Write it like this

The code is as follows:

Foreach ($ array as & $ row ){
$ Row = explode ('/', $ row );
}
Unset ($ row );
Foreach ($ array as $ row ){
// Do something
}

Or write in the first loop.

Foreach ($ array as $ key => $ row ){
$ Array [$ key] = explode ('/', $ row );
}

The values statement is used to traverse the array cyclically. each time a loop is executed, the value of the current array element will be assigned to the value variable (the array pointer will move down one by one). example code is as follows...

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.