What is For each Loops?
Use the for each
loops to cycle through arrays. At each pass, the value of the current array element is set to $value. In the next pass, the array pointer is moved one by one. The for each
loop is only used to loop around each key/values pair in an array. Only arrays are supported.
Syntax:-
foreach ($array as $value) {
code to be executed;
}
Here are some of the Foreach Loops:-
- “foreach(…){…}” is the foreach php loop block code
- “$array_data” is the array variable to be looped through
- “$array_value “ is the temporary variable that holds the current array item values.
- “block of code…” is the piece of code that operates on the array values
Example1:-
Output:-
Example2:-
Output:-