Php array traversal. Php array traversal 1, B1, C1, D1); foreach ($ arrayas $ key $ value) {if ($ keyB) {$ array [A] CHANGE; $ array [D] CHANGE; print_r ($ array); echo;} if you want to print CHNAGE, php array traversal
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 '';} // If you want to print CHNAGE, then you can use // if ($ array [$ key] = 'change') // the actual element value of the array is retrieved from the above, $ value is used to retrieve a copy of the original value of the array if ($ value = CHANGE) echo $ value. '';} print_r ($ array ); * // ** The result is * Array ([A] => CHANGE [B] => 1 [C] => 1 [D] => CHANGE) array ([A] => CHANGE [B] => 1 [C] => 1 [D] => CHANGE) **/$ 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 '';} // If you want to print CHNAGE, you can use if ($ array [$ key] = 'change') echo $ value. '';} print_r ($ array ); /** The result is * Array ([A] => CHANGE [B] => 1 [C] => 1 [D] => CHANGE) 1 (here 1 is the value corresponding to the original array key) array ([A] => CHANGE [B] => 1 [C] => 1 [D] => CHANGE )*/
Note: Unless the array is referenced, the foreach operation performs a copy of the array, not the array itself, in addition, the modification to the array element during the traversal process does not affect the copy (it feels like an array is copied once before the traversal, and the traversal operation is an array copy ). Foreach has some side effects on the array pointer. unless it is reset, the value of the array pointer should not be dependent in the foreach loop or after the loop. In foreach, you only need to perform various assignment operations based on the elements in the $ array key.
See the php foreach manual
The foreach syntax structure provides a simple way to traverse arrays. Foreach can only be applied to arrays and objects. if you try to apply it to variables of other data types or uninitialized variables, an error message will be sent. There are two syntaxes:
foreach (array_expression as $value) statementforeach (array_expression as $key => $value) statement
The first format traverses the given array_expression array. In each loop, the value of the current unit is assigned to $ value (create a copy) and the pointer inside the array moves forward (so the next unit will be obtained in the next loop ).
In the second format, only the key names of the current unit are assigned to the variable $ key (create a copy) in each loop ).
Note:
When foreach starts to execute, the pointer inside the array automatically points to the first unit. This means that you do not need to call reset () before the foreach loop ().
Because foreach depends on the internal array pointer, modifying its value in the loop may lead to unexpected behavior.
You can easily add & before $ value to modify the elements of the array. This method will assign values by reference instead of copying a value.
Note: The $ value reference of the last element of the array is retained after the foreach loop. We recommend that you use unset () to destroy it.
Values 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;} // If you want to print CHNAGE, then...