PHP Array foreach


The PHP foreach() syntax is used to traverse an operation or an output array, and foreach() can only be used to iterate over an array or object. An error occurs when trying to use it for other data types or an uninitialized variable.

grammar:

foreach(array as $value)
    // Statement
// Alternatively:
foreach(array as $key => $value)
    // Statement

In the syntax above, the current cell is assigned the value $value and the pointer inside the array is moved one step at a time. In the second syntax, the key name of the current cell is also assigned to the variable $key in each iteration.

example:

$arr_age = array("jack" => 18, "jack" => 20, "marry" => 25);
foreach($arr_age as $age) {
    echo $age, '<br />';
}

Run the example output:

18
20
25

Use the array key

$arr_age = array("jack" => 18, "jack" => 20, "marry" => 25);
foreach($arr_age as $key => $age) {
    echo $key, ':', $age, '<br />';
}

Example output:

John: 18
Jack: 20
Marry: 25

Tip

When the foreach begins execution, the pointer inside the array automatically points to the first element, which means that you do not need to call reset() before the foreach loop.

Foreach operates on a copy of the specified array, not the array itself. Modifications to the returned array elements do not affect the original array(see example below), but the foreach loop runs to the end, and the internal pointer of the original array points to the end of the array.

$arr_age = array("jack" => 18, "jack" => 20, "marry" => 25);
foreach($arr_age as $age) {
    $age = $age + 10;
    echo $age, '<br />';
}
// Output the original array
print_r($arr_age);

Example output:

28
30
35
array(
  [jack] => 18 
  [john] => 20 
  [marry] => 25
)

To modify the original array elements in foreach, you can achieve by reference, the above example is changed to:

$arr_age = array("jack" => 18, "jack" => 20, "marry" => 25);
foreach($arr_age as & $age) {
    $age = $age + 10;
    echo $age, '<br />';
}
// Output the original array
print_r($arr_age);

Example output:

18
20
25
array(
  [jack] => 28 
  [john] => 30 
  [marry] => 25
)

Traverse multidimensional arrays

Foreach syntax structure can only be used to traverse a one-dimensional array, to traverse a multidimensional array, usually foreach nested recursive use or the original array is divided into a one-dimensional array and then foreach traversal.

A two-dimensional array of mixed examples:

$arr_age = array("jack" => 18, "john" => 20, "marry" => array("name" => "david", "age" => 25));
foreach($arr_age as $age) {
    if(is_array($age)) {
        foreach($age as $detail) {
        echo $detail, '<br />';
        }
    } else {
        echo $age, '<br />';
    }
}

Run the example output:

18
20
david
25

The traversal of multi-dimensional array of data processing will be based on the actual structure and take the most appropriate approach.

PHP array through the hash table(HashTable) table to achieve, so foreach traversal of the array is based on the order of elements added to the. If you want to traverse the size of the index, you should use for () loop traversal.


for () loop through an array

For an array of consecutive key-values ​​, the for () loop can also be used to loop through an array:

$arr_age = array(18, 20, 25);
$Num = count($arr_age);
for ($i = 0; $i < $num; $i ++) {
    echo $arr_age [$i]. " <br /> ";
}

The running example output is as follows:

18
20
25

Tip

You can also use list() and each() combination to traverse the array, but the test found that efficiency is not as good as foreach().




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook