PHP Array traversal

Source: Internet
Author: User
Tags php foreach

<?php/* * This program is the operation of the PHP array, mainly in the time elapsed to modify the value of the array *//* $array = Array ("A" =>1, "B" =>1, "C" =>1, "D" =>1); foreach ($array        as $key = = $value) {if ($key = = "B") {$array ["A"] = "change";        $array ["D"] = "change";        Print_r ($array);    echo ' <br/> ';  }//If you want to print chnage, you can use//if ($array [$key] = = ' change ')///above to remove the value of the actual element of the array, using $value to remove the original value of the array array is a copy of the IF ($value = = = "Change") echo $value. ' <br/> ';} Print_r ($array); *//* * The result is an array ([a] = change [B] = 1 [C] = + 1 [D] = change) array ([A] = CHAN GE [B] = 1 [c] = 1 [D] = change) * */$array = Array ("A" =>1, "B" =>1, "C" =>1, "D" =>1); foreach ($arr        ay as $key = = $value) {if ($key = = "B") {$array ["A"] = "change";        $array ["D"] = "change";        Print_r ($array);    echo ' <br/> '; }//If you want to print chnage, you can use the IF ($array [$key] = = ' Change ') echo $value. ' <br/> ';} Print_r ($array);/* * The result is an array ([A] = Change [B] = 1 [C] = 1 [D] = = change) 1 (here 1 is the value corresponding to the original key of the array) array ([A] = change [B] = 1 [c] = 1 [D] = change) */

Note: unless the array is referenced, the foreach operation is to execute a copy of the array, not the array itself, and the modification of an array element during the traversal does not affect the copy (it feels like a copy of the arrays before the traversal, and a copy of the array at the time of the traversal) . foreach has some side effects on array pointers, and unless you reset them, do not rely on the values of the array pointers in the Foreach loop or after the loop. as long as in the foreach, directly follow the key to take the elements of the $array in a variety of judgment assignment operation is OK.


See the PHP foreach manual

The foreach syntax structure provides a simple way to pass through the group. foreach can only be applied to arrays and objects, if you try to apply a variable to another data type, or an uninitialized variable will emit an error message. There are two kinds of syntax:

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

The first format iterates through the given array of array_expression . In each loop, the value of the current cell is assigned to the $value (a copy is created) and the pointer inside the array moves forward one step (so the next cell in the next loop will be taken).

The second format does the same thing, except that the key name of the current cell is assigned to the variable $key ( create a copy )in each loop.

Note:

when foreach when execution starts, the pointer inside the array automatically points to the first cell. This means that you do not need to call Reset () before the Foreach Loop .

Because foreach relies on an internal array pointer, modifying its value in a loop can cause unexpected behavior.

You can easily modify the elements of an array by adding & to it before $value. This method assigns a value to a reference instead of copying a value.

<?php$arr  = Array (1,  2,  3,  4), foreach ($arr as  & $value) {     $value  =  $value 
   *  2;} $arr is now Array (2, 4, 6, 8) unset ($value);  Finally, remove the reference?>

Note: The $value Reference of the last element of the array remains after the foreach loop. It is recommended to use unset () to destroy it.





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

PHP Array traversal

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.