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<?php
2$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
3
4foreach($age as $x => $val) {
5 echo "$x = $val<br>";
6}
7?>
1
2<?php
3$arr = array(1, 2, 3, 4);
4foreach ($arr as &$value) {
5 $value = $value * 2;
6}
7// $arr is now array(2, 4, 6, 8)
8
9// without an unset($value), $value is still a reference to the last item: $arr[3]
10
11foreach ($arr as $key => $value) {
12 // $arr[3] will be updated with each value from $arr...
13 echo "{$key} => {$value} ";
14 print_r($arr);
15}
16// ...until ultimately the second-to-last value is copied onto the last value
17
18// output:
19// 0 => 2 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 2 )
20// 1 => 4 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 4 )
21// 2 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 )
22// 3 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 )
23?>
24
25
1<?php
2$food = array('burger','pizza','golgappa','momoes');
3foreach($food as $value)
4{
5 echo $value,"<br>";
6}
7?>