1foreach (array as $value){
2 //code to be executed;
3 print("value : $value");
4}
5
6foreach (array as $key => $value){
7 //code to be executed;
8 print("key[$key] => $value");
9}
1$arr = array(
2 'key1' => 'val',
3 'key2' => 'another',
4 'another' => 'more stuff'
5);
6foreach ($arr as $key => $val){
7 //do stuff
8}
9
10//or alt syntax
11foreach ($arr as $key => $val) :
12 //do stuff here as well
13endforeach;
14
1<?php
2// Declare an array
3$arr = array("green", "blue", "pink", "white");
4
5// Loop through the array elements
6foreach ($arr as $element) {
7 echo "$element ";
8}
9?>
1foreach (array_expression as $value)
2 statement
3foreach (array_expression as $key => $value)
4 statement