Use of foreach

Source: Internet
Author: User
foreach loop statements, commonly used to iterate over an array, generally have two ways to use: Do not remove the label, remove the subscript//Do not remove the table foreach (array as value) {   //execute the program    echo value;} Remove the subscript foreach (array as subscript = = value) {   //Execute program    echo $key = = $value;    echo "<BR>;} foreach iterates through two-dimensional arrays: <?php      $arr = Array ("Phpbook" =>array (5=> "php", "PHP Network Programming", "Php&&mysql Programming"));      foreach ($arr as $key = + $value)      {          echo $key;          echo ': ';          foreach ($value as $key 1 + $value 1)              {                      echo "$key 1 = $value 1";              }      }//multidimensional array similar, Just notice that the array name in the nth foreach is the value of the n-1: foreach ($value (n-1) as $key (n) + $value (n))//array subscript index is the numeric index by default when not indicated, and the default is 1 growth from 0. If the first one is 5, then the next subscript starts with 5, automatically adds the conditional structure in 1foreach: foreach ($students as $key + $value) {    if (condition)    {         //execute code        break;//continue;}     }

  

Use of foreach

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.