PHPforeach loop Usage Details and instance code

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

In PHP, foreach is used to loop all elements of an array.

The basic syntax of foreach is as follows:
FOREACH ($ array_variable as $ value)
{
[Code to execute]
}
Or
FOREACH ($ array_variable as $ key => $ value)
{
[Code to execute]
}
In these two cases, multiple [Code executions] will be executed equal to the number of elements in the $ array_variable array.
Let's look at an example. Suppose we have the following code segment:
The Code is as follows:
$ Array1 = array (1, 2, 3, 4, 5 );
FOREACH ($ array1 as $ abc)
{
Print "new value is". $ abc * 10 ."
";
}

Output result
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 five elements, and each time a declaration containing 10 times the value of the array element is printed.
Foreach traverses all values of the current array and assigns them to $ var.
Official instance code:
The 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 authentication )*/
$ A = array (1, 2, 3, 17 );
$ I = 0;/* for each strative 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-dimen1_arrays */
$ A = array ();
$ A [0] [0] = "";
$ A [0] [1] = "B ";
$ A [1] [0] = "y ";
$ A [1] [1] = "z ";
Foreach ($ a as $ v1 ){
Foreach ($ v1 as $ v2 ){
Echo "$ v2 \ n ";
}
}
/* Foreach example 5: dynamic arrays */
Foreach (array (1, 2, 3, 4, 5) as $ v ){
Echo "$ v \ n ";
}
?>

Let's look at a foreach multi-dimensional data operation instance.
The Code is as follows:
$ S = array (1, 2), array (3, 4), array (5, 6 ));
Foreach ($ s as $ v =>$ _ v)
{
Foreach ($ _ v as $ vc =>$ _ vc)
{
Echo $ _ vc [0], '|'. $ _ vc [1],'
';
// Print_r ($ _ vc );
}
}

Let's take a look at the foreach Syntax:
The Code is as follows:
Foreach ($ array as $ key => $ value)
{
......
}

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: next ($ array ), move the array Pointer Forward (the "Forward" here is the same as the PHP manual, not the "Forward" We generally understand) to one. Then, the echo statement outputs "first => ibm ".
The second loop first checks whether the current pointer of array $ array has reached the end of the array. If yes, it ends the loop. Otherwise, it enters the second loop. Of course, this judgment will also occur when you enter the first loop. If $ array is an empty array, the statements below the loop will be executed directly. In this case, $ key = 'second', 'value' = 'hp ', next ($ array), and output "second => hp ". Then, judge that the array pointer has reached the end and execute the following statement.
Now you should have some knowledge about the foreach loop? Another point is that foreach only transmits a value at a time, rather than performing operations on array elements. Specific to the example above, if you want to add the 'company' string after each array element, then $ value. = 'company' does not work, and it does not change the value of the array element. In this case, you should use: $ array [$ key]. = 'company ';

For more information, see:

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.