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$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
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)
8unset($value); // break the reference with the last element
9?>
10
11
1
2<?php
3
4$a = array(1, 2, 3, 17);
5
6foreach ($a as $index => $v) {
7 echo "Current value of \$a: $v.\n";
8}
9
10?>
11
12