PHP foreach Loop using detailed and instance code _php tutorial

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 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:
Copy CodeThe code is as follows:
$array 1 = array (1,2,3,4,5);
FOREACH ($array 1 as $ABC)
{
Print "New value is". $abc *10. "
";
}

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:
Copy CodeThe code is as follows:
/* 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,
"Both" = 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
Copy CodeThe code is as follows:
$s = Array (array), array (3,4), Array (5,6));
foreach ($s as $v = $_v)
{
foreach ($_v as $VC = $_VC)
{
Echo $_vc[0], ' | '. $_vc[1], '
';
Print_r ($_VC);
}
}

First look at the syntax of foreach:
Copy CodeThe code is as follows:
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:
Copy CodeThe code is as follows:
foreach ($array as $key = $value)
{
echo $key. ' = '. $value;
}

The first loop, $key = ' primary ', $value = ' IBM ', this time, the actual foreach performs an operation we can't see for $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.
You should know something about the Foreach loop here, 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 ';

See also: PHP foreach
PHP foreach, while performance comparison
PHP loops

http://www.bkjia.com/PHPjc/321830.html www.bkjia.com true http://www.bkjia.com/PHPjc/321830.html techarticle 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, more ...

  • 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.