Foreach Loop in PHP
In PHP associative array gives us more power to use arrays in more effective way, like we can associate any key with a value. To fetch values from associative array we need to use for each loop.
Using for each loop we can assign values of one by one to a variable and that variable is used inside the loop for various purpose like displaying the values of array, doing mathematical calculation etc.
Nowadays for-each loop is being used by almost every language like Java, C# etc.
Following examples will help you to learn for each loop precisely:
Example:
<?php
$studentClass
;$studentClass
["ram"]="5";$studentClass
["rahim"]="5";$studentClass
["mohan"]="4";$studentClass
["joseph"]="5";$studentClass
["salma"]="2";foreach
($studentClass as $key => $value){ echo "$key reads in class $value <br/>";}?>
Output:
ram reads in class 5
rahim reads in class 5
mohan reads in class 4
joseph reads in class 5
salma reads in class 2