1$arr = ['Item 1', 'Item 2', 'Item 3'];
2
3foreach ($arr as $item) {
4 var_dump($item);
5}
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<?php
2$arr = ['Item 1', 'Item 2', 'Item 3'];
3
4foreach ($arr as $item) {
5 var_dump($item);
6}
7
8$dict = array("key1"=>"35", "key2"=>"37", "key3"=>"43");
9
10foreach($dict as $key => $val) {
11 echo "$key = $val<br>";
12}
13?>
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$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;
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?>