PHP foreach Loop usage and instance code _php instance

Source: Internet
Author: User
Tags php foreach
The foreach basic syntax is as follows:
FOREACH ($array _variable as $value)
{
[Code to execute]
}
Or
FOREACH ($array _variable as $key => $value)
{
[Code to execute]
}
In both cases, multiple [code executions] will be executed 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:
Copy Code code as follows:

$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 goes through the array $ array1 of all 5 elements, each time printing out a statement containing 10 times times the value of the array element.
The foreach action is to iterate through all the values of the current array and assign it to $var.
The official example code:
Copy Code code as follows:

<?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 and value * *
$a = Array (
"One" => 1,
"Two" => 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";
}
?>

Let's look at a foreach. Instance of multidimensional data operation
Copy Code code as follows:

$s = Array (array (1,2), 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);
}
}

Let's take a look at the syntax of foreach:
Copy Code code as follows:

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

For the sake of understanding, we assume that the $array here is a one-dimensional array of related, $key is the index of the array, $value is the value of the index, their names can be arbitrary, the reason why $key and $value are called to facilitate understanding. To give you a better understanding of foreach's working process, let's create an array:
$array = Array (' => ' IBM ', ' second ' => ' hp ');
Now we simulate the PHP service using foreach to traverse the $array:
Copy Code code as follows:

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

The first cycle, $key = ' a ', $value = ' IBM ', this time, the actual foreach performs an invisible operation on $array: Next ($array), forward the array pointer (here "forward" is the same as the PHP manual, Not what we normally understand as "forward") move one. The Echo statement then outputs "FIRST=&GT;IBM".
The second loop first determines whether the current pointer to the array $array is at the end of the array and, if so, ends the loop, otherwise enters the second loop. Of course, such judgments are also available when entering the first cycle, and if $array is an empty array, the following statements are executed directly from the loop. At this point, the output "second=>hp" is $key = ' second ', ' value ' = ' hp ', Next ($array). Then make a judgment that the array pointer has reached the end, executing the following statement.
You should know something about the Foreach loop here, right? What is needed is that foreach passes only one value at a time, instead of the real array element being manipulated. Specifically to the example above, if you want to add a string of ' company ' to the back of each array element, then $value. = ' Company ' does not work, it does not change the value of the array elements, this time you should use this: $array [$key]. Company ';

More can be referred to: PHP foreach
PHP foreach, while performance comparison
PHP loop

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.