Learn PHP


for each loop


The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:


	      	foreach (array_expression as $value)
			    statement
			foreach (array_expression as $key => $value)
			    statement
	      

The first form loops over the array given by array_expression. On each iteration, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next iteration, you'll be looking at the next element).


The second form will additionally assign the current element's key to the $key variable on each iteration.


In PHP 5, when foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop.


As foreach relies on the internal array pointer in PHP 5, changing it within the loop may lead to unexpected behavior.


In PHP 7, foreach does not use the internal array pointer.


		  	<?php
			$arr = array(1, 2, 3, 4);
			foreach ($arr as &$value) {
			    $value = $value * 2;
			}
			// $arr is now array(2, 4, 6, 8)
			unset($value); // break the reference with the last element
			?>
		  


example


		  	<?php
			$arr = array(1, 2, 3, 4);
			foreach ($arr as &$value) {
			    $value = $value * 2;
			}
			// $arr is now array(2, 4, 6, 8)

			// without an unset($value), $value is still a reference to the last item: $arr[3]

			foreach ($arr as $key => $value) {
			    // $arr[3] will be updated with each value from $arr...
			    echo "{$key} => {$value} ";
			    print_r($arr);
			}
			// ...until ultimately the second-to-last value is copied onto the last value

			// output:
			// 0 => 2 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 2 )
			// 1 => 4 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 4 )
			// 2 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 )
			// 3 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 )
			?>
		  

example


		  	<?php
			foreach (array(1, 2, 3, 4) as &$value) {
			    $value = $value * 2;
			}
			?>
		  

foreach does not support the ability to suppress error messages using '@'.


example


		  	<?php
			/* foreach example 1: value only */

			$a = array(1, 2, 3, 17);

			foreach ($a as $v) {
			    echo "Current value of \$a: $v.\n";
			}

			/* foreach example 2: value (with its manual access notation printed for illustration) */

			$a = array(1, 2, 3, 17);

			$i = 0; /* for illustrative purposes only */

			foreach ($a as $v) {
			    echo "\$a[$i] => $v.\n";
			    $i++;
			}

			/* foreach example 3: key and value */

			$a = array(
			    "one" => 1,
			    "two" => 2,
			    "three" => 3,
			    "seventeen" => 17
			);

			foreach ($a as $k => $v) {
			    echo "\$a[$k] => $v.\n";
			}

			/* foreach example 4: multi-dimensional arrays */
			$a = array();
			$a[0][0] = "a";
			$a[0][1] = "b";
			$a[1][0] = "y";
			$a[1][1] = "z";

			foreach ($a as $v1) {
			    foreach ($v1 as $v2) {
			        echo "$v2\n";
			    }
			}

			/* foreach example 5: dynamic arrays */

			foreach (array(1, 2, 3, 4, 5) as $v) {
			    echo "$v\n";
			}
			?>
		  


exaample


		  	<?php
			$array = [
			    [1, 2],
			    [3, 4],
			];

			foreach ($array as list($a, $b)) {
			    // $a contains the first element of the nested array,
			    // and $b contains the second element.
			    echo "A: $a; B: $b\n";
			}
			?>
		  

exaample


		  	<?php
			$array = [
			    [1, 2],
			    [3, 4],
			];

			foreach ($array as list($a)) {
			    // Note that there is no $b here.
			    echo "$a\n";
			}
			?>
		  

exaample


		  	<?php
			$array = [
			    [1, 2],
			    [3, 4],
			];

			foreach ($array as list($a, $b, $c)) {
			    echo "A: $a; B: $b; C: $c\n";
			}
			?>
		  


exaample


		  	<?php
			$arr1 = array("a" => 1, "b" => 2, "c" => 3);
			$arr2 = array("x" => 4, "y" => 5, "z" => 6);

			foreach ($arr1 as $key => &$val) {}
			foreach ($arr2 as $key => $val) {}

			var_dump($arr1);
			var_dump($arr2);
			?>
		  

exaample


		  	<?php

			$array = [ 'one', 'two', 'three', 'four', 'five' ];

			foreach( $array as $value ){
			    if( $value == 'three' ){
			        echo "Number three was found!";
			        break;
			    }
			}

			?>
		  

exaample


		  	<?php 
			$arr = array(1,2,3,4,5,6,7,8,9); 

			foreach($arr as $key=>$value) 
			{ 
			    unset($arr[$key + 1]); 
			    echo $value . PHP_EOL; 
			} 
			?> 
			Output: 
			1 2 3 4 5 6 7 8 9 

			<?php 
			$arr = array(1,2,3,4,5,6,7,8,9); 

			while (list($key, $value) = each($arr)) 
			{ 
			    unset($arr[$key + 1]); 
			    echo $value . PHP_EOL; 
			} 
			?> 
		  




PHP Tutorials


Home

Introduction

Syntax

Variable

Comment

Echo and Print Statement

Data Types

Boolean

Integer

Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion

file i/o