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