The foreach loop is used to loop through arrays.

Syntax:

foreach ($array as $value)
{
code to be executed;
}

foreach ($array as $key => $value)
{
code to be executed;
}

For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) – so on the next loop iteration, you’ll be looking at the next array value.

Example1:
Code:

$arr = array(4,6,8,10);
foreach ($arr as &$value) {
    $value = $value * 3;
	echo $value;
}

Output:

12 18 24 30

Example2:
Code:

 &$value) {
   $value = $value * 3;
	echo "key: $key, value: $value 
"; }

Output:

key: 0, value: 12
key: 1, value: 18
key: 2, value: 24
key: 3, value: 30

eXTReMe Tracker