foreach Loop Usage Instance code

Source: Internet
Author: User
In PHP, foreach is used to loop through all the elements of an array.

The foreach basic syntax is as follows:

foreach ($array _variable as $value) {[Code to execute]} or FOREACH ($array _variable as $key = $value) {[Code to EXE Cute]}

In both cases, multiple [code execution] will be executed to be equal to the number of elements in the $ array_variable array.
Let's take a look at an example. Let's say we have the following code snippet:

$array 1 = array (1,2,3,4,5); FOREACH ($array 1 as $ABC) {print "New value is". $abc *10. "<br>"; }

Output results
New value is 10
New value is 20
New value is 30
New value is 40
New value is 50
The Foreach loop above has experienced all 5 elements of the array $ array1, each time printing out a declaration containing 10 times times the value of the array element.
The Foreach function iterates through all the values of the current array and assigns them to $var.
Example code given by the official:

<?php/* Foreach example 1:value only */$a = array (1, 2, 3, 17);  foreach ($a as $v) {echo "Current value of \ $a: $v. \ n";}/* foreach example 2:value (with key printed for illustration) */$a = array (1, 2, 3, 17); $i = 0; /* For illustrative purposes only */foreach ($a as $v) {echo "\ $a [$i] + $v. \ n"; $i + +;}/* foreach example 3:key an D value */$a = array ("One" = 1, "One" = 2, "three" = 3, "Seventeen" = 17); foreach ($a as $k + $v) {echo "\ $a [$k] = $v. \ n";}/* foreach example 4:multi-dimensional arrays */$a = array () ; $a [0][0] = "a"; $a [0][1] = "B"; $a [1][0] = "Y"; $a [1][1] = "Z"; foreach ($a as $v 1) {foreach ($v 1 as $v 2) {echo "$v 2\n"}}/* foreach example 5:dynamic arrays */foreach (Array (1, 2 , 3, 4, 5) as $v) {echo "$v \ n";}?>

Take another look at a foreach-to-multidimensional data manipulation instance

$s = Array (array), array (3,4), Array (5,6)); foreach ($s as $v = $_v) {foreach ($_v as $VC = $_VC) {echo $_vc[0], ' | '. $_vc[1], ' <br/> '; Print_r ($_VC); } }

First look at the syntax of foreach:

foreach ($array as $key = = $value) {...}

For the sake of understanding, we assume that the $array here is a one-dimensional related array, $key is the index of the array, $value is the value of the index, their names can be arbitrary, the reason is called $key and $value for easy understanding. In order to give you a better understanding of the working process of foreach, let's create an array:
$array = Array (' first ' = ' = ' IBM ', ' second ' = ' hp ');
Now we are simulating PHP services using foreach to traverse the $array:

foreach ($array as $key = + $value) {echo $key. ' = '. $value; }

First loop, $key = ' primary ', $value = ' IBM ', this time, the actual foreach performs an invisible operation on $array: Next ($array), move the array pointer forward (here "forward" is the same as the PHP manual, Not what we usually understand as "forward") move one. The Echo statement then outputs "FIRST=>IBM". The
second loop, first determines whether the current pointer to the array $array has reached the end of the array, or, if it is, ends the loop, otherwise enters the second loop. Of course, such a judgment will be in the first cycle of the time, if $array is an empty array, then the loop will be executed directly below the statement. At this point, $key = ' second ', ' value ' = ' hp ', Next ($array), output "second=>hp". Then make a judgment, the array pointer has reached the end, execute the following statement.
Here you should know something about the Foreach Loop, right? Another thing you need is that foreach is simply passing one value at a time instead of actually manipulating the array element. Specifically to the above example, if you want to add the string "Company" after each array element, then $value. = ' Company ' does not work, it does not change the value of the array element, this time you should use: $array [$key]. Company ';

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.