PHP foreach Loop instance foreach usage

Source: Internet
Author: User
Tags foreach php foreach

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:


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

Let's look at a foreach. Instance of multidimensional data operation

  $s = array (array (1,2), array (3,4), Array (5,6));
 foreach ($s as $v => $_v)
  {
   foreach ($_v as $VC => $_vc)
&nbs p;  {
    echo $_vc[0], ' | '. $_vc[1], ' <br/> ';
   //print_r ($_VC);
   }
  }
This article is original in Www.111cn.net Reprint annotated Origin
*/

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.