PHP array traversal foreach syntax structure ____php

Source: Internet
Author: User
Tags php foreach

Source: http://www.cnblogs.com/keta/p/6117237.html

The foreach syntax structure is used to traverse an array. foreach ()

The PHP foreach () syntax structure is used to traverse an array of operations or output, and foreach () can only be used to traverse an array or object, and an error occurs when an attempt is made to use it for another data type or an uninitialized variable.

Grammar:

foreach (array as $value)
    statement
//or:
foreach (array as $key => $value)
    statement

In the syntax above, each loop assigns the value of the current cell to the $value and the pointer inside the array moves forward one step. The key name of the current cell is also assigned to the variable $key in each loop in the second syntax format.

Example:

<?php
$arr _age = Array ("Wang" =>18, "Li" =>20, "Zhang" =>25);
foreach ($arr _age as $age) {
    echo $age, ' <br/> ';
}
? >

Run the example output:

25
using array key values
<?php
$arr _age = Array ("Wang" =>18, "Li" =>20, "Zhang" =>25);
foreach ($arr _age as $key => $age) {
    echo $key, ': ', $age, ' <br/> ';
}
? >

Run Example output:

Wang:18
li:20
zhang:25
Tips

When foreach starts executing, the pointer inside the array automatically points to the first cell, which means that reset () is not required before the Foreach loop.

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

<?php
$arr _age = Array ("Wang" =>18, "Li" =>20, "Zhang" =>25);
foreach ($arr _age as $age) {
    $age = $age +10;
    echo $age, ' <br/> ';
}
Outputs the original array
print_r ($arr _age);
? >

Run Example output:

([Wang] => [Li] => [Zhang] => 25) 

To modify the original array element in foreach, you can do so by reference, changing the example above to:

<?php
$arr _age = Array ("Wang" =>18, "Li" =>20, "Zhang" =>25);
foreach ($arr _age as & $age) {
    $age = $age +10;
    echo $age, ' <br/> ';
}
Outputs the original array
print_r ($arr _age);
? >

Run Example output:

([Wang] => [Li] => [Zhang] => 35) 
traversing multidimensional Arrays

The foreach syntax structure can only be used to traverse a one-dimensional array, to traverse a multidimensional array, typically by using a foreach nested recursive or by dividing the original array into a one-dimensional array for a foreach traversal.

Example of one or two-D array blending:

$arr _age = Array ("Wang" =>18, "Li" =>20, "Zhang" =>array ("name" => "Xiao Zhang", "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:

Small
25

The traversal processing of multidimensional array should be based on the actual data structure to take the most appropriate approach. extend pre-read

PHP arrays are implemented through the hash table (HashTable) tables, so foreach traverses the array based on the order in which the elements are added. If you want to traverse by index size, you should use a for () loop traversal. For () looping through an array

If you are an array that operates on consecutive key values, you can also use a for () loop to traverse an array:

<?php
$arr _age = Array (a);
$num = count ($arr _age);
for ($i = 0; $i < $num; $i + +) {
    echo $arr _age[$i]. " <br/> ";
}
? >

The running example output is as follows:

25
Tips

You can also use a combination of list () and each () to traverse an array, but the test finds less efficient than foreach ().

Use the array () statement structure to declare all the data in the contact list as a two-dimensional array, with the default subscript being the sequential numeric index $contact 1 = array (//= outer number defined) Group Array (1, ' High certain ', ' a company ', ' Beijing ', ' (010) 987654321 ', ' gm@Linux.com '),//Sub array 1 array (2, ' Luo mou ', ' B Company ', ' Shanghai City ', ' (021) 123456789 ', ' Lm@apache.com '),//Sub array 2 array (3, ' peak some ', ' C Company ', ' Tianjin ', ' (022) 24680246 ', ' fm@mysql.com '),//Sub Array 3 Array (4, ' Book a ', ' d company ', ' Chongqing ',
   ' (023) 13579135 ', ' sm@php.com ')//Sub array 4);
    Output each element in a two-dimensional array as an HTML table echo ' <table border= 1 "width=" align= "center" >;
    Echo ' <caption>



$contact 1 = array (//definition outer array)
Array (1, ' Gao MoU ', ' A company ', ' Beijing ', ' (010) 987654321 ', ' gm@Linux.com '),//Sub array 1
Array (2, ' Luo mou ', ' B Company ', ' Shanghai City ', ' (021) 123456789 ', ' lm@apache.com '),//Sub array 2
Array (3, ' peak some ', ' C Company ', ' Tianjin ', ' (022) 24680246 ', ' fm@mysql.com '),//Sub Array 3
Array (4, ' Book a ', ' d company ', ' Chongqing ', ' (023) 13579135 ', ' sm@php.com ')//Sub Array 4
);

foreach ($contact 1 as $key => $s) {

echo $key;//With the key value of each array as the table name
foreach ($s as $row) {

Echo $row;


}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.