PHP array traversal foreach syntax structure and examples

Source: Internet
Author: User
Tags php foreach
foreach ()

The PHP foreach () syntax structure is used to traverse an operation or an output array, and foreach () can only be used to iterate over an array or an object, resulting in an error when trying 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 above syntax, each loop assigns the value of the current cell to the $value and the pointer inside the array moves forward one step. In the second syntax format, the key name of the current cell is also assigned to the variable $key in each loop.

Example:

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

Run the example output:

182025

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 the example output:

Wang:18li:20zhang:25

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

A foreach operation is a copy of the specified array, not the array itself. Changes to the returned array cell do not affect the original array (see example below), but the Foreach loop runs to the end, and the inner pointer of 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/> ';} Output original array print_r ($arr _age);? >

Run the example output:

283035Array ([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 + Ten;  echo $age, ' <br/> ';} Output original array print_r ($arr _age);? >

Run the example output:

182025 Array ([Wang] [li] = [Zhang] + 35)

Traversing multidimensional arrays

The foreach syntax structure can only be used to traverse a one-dimensional array, traversing a multidimensional array, typically recursively using foreach nesting or splitting the original array into a one-dimensional array and then a foreach traversal.

Examples of one or two-D array blending:

<?php$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/> ';    }  } els e {    echo $age, ' <br/> ';  }}? >

The traversal processing of multidimensional arrays is based on the actual data structure and the most appropriate processing method.

The PHP array is implemented through a Hashtable (HashTable) table, so the foreach traversal of the array is 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 successive key values, you can also use the for () loop to iterate through the array:

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

You can also use list () and each () to iterate through the array, but the test finds efficiency

Rather than foreach ().

This PHP array traversal of the Foreach syntax structure and examples is the small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we have a lot of support topic.alibabacloud.com.

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.