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 = array(1, 2, 3, 4);
3foreach ($arr as &$value) {
4 $value = $value * 2;
5}
6// $arr is now array(2, 4, 6, 8)
7unset($value); // break the reference with the last element
8?>
1
2<?php
3foreach (array(1, 2, 3, 4) as &$value) {
4 $value = $value * 2;
5}
6?>
7
8
1
2<?php
3$array = [
4 [1, 2],
5 [3, 4],
6];
7
8foreach ($array as list($a, $b)) {
9 // $a contains the first element of the nested array,
10 // and $b contains the second element.
11 echo "A: $a; B: $b\n";
12}
13?>
14
15