-->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 ')//The value of the actual element that is taken out of the array, using $value to take out a copy of the original value of array arrays if ($v
Alue = = change) echo $value. '
';
} print_r ($array); */* 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 the IF ($array [$key] = = ' Change ') echo $value. '
';
} print_r ($array); * * result is * array ([a] => change [B] => 1 [C] => 1 [D] => change) 1 (1 is the corresponding value of the original key of the array) array ([a] =&G T Change [B] =>1 [C] => 1 [D] => Change) * *
Note: Unless the array is referenced, a foreach operation is a copy of the execution array, not the array itself, and the modification of the elements during the traversal does not affect the copy (it feels like a copy of the array is made once before the traversal, and an array copy is performed when traversing). The foreach array pointer has side effects that, unless reset, do not rely on the value of the array pointer in the Foreach loop or after the loop. As long as in the foreach, directly according to the key to take the elements in the $array to determine the value of the operation can be.
See PHP foreach manual
The foreach syntax structure provides a simple way to traverse an array. foreach can only be applied to arrays and objects, if an attempt is made to apply to a variable of another data type, or an uninitialized variable emits an error message. There are two kinds of syntax:
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
The first form traverses the given array_expression array. In each loop, the value of the current cell is assigned to the $value (create a copy) and the pointer inside the array moves forward one step (so the next cell will be in the next loop).
The second format does the same thing, except that the key name of the current cell is also assigned to the variable $key (create a copy) in each loop.
Note:
When foreach starts executing, 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 result in unexpected behavior.
You can easily modify the elements of an array by adding & before the $value. This method assigns a value to a reference instead of copying a value.
Note: The $value reference of the last element of the array remains after the Foreach loop. It is recommended that you use unset () to destroy it.