Home > Article > Backend Development > php array traversal_PHP tutorial
<!--?php /* * 此程序是对php数组的操作,主要是在遍历时修改数组的值 */ /* $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 ' '; } //如果想要打印CHNAGE,那么可以使用 //if($array[$key] == 'CHANGE') //上面的取出的才是数组实际元素的值,使用$value取出的是array数组的原始值的副本 if($value === CHANGE) echo $value.' '; } print_r($array); */ /* * 结果是 * 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 ' '; } //如果想要打印CHNAGE,那么可以使用 if($array[$key] == 'CHANGE') echo $value.' '; } print_r($array); /* * 结果是 * Array ( [A] => CHANGE [B] => 1 [C] => 1 [D] => CHANGE ) 1(这里的1是数组原始的键对应的值) Array ( [A] => CHANGE [B] => 1 [C] => 1 [D] => CHANGE ) */
Note: Unless the array is referenced, the foreach operation executes a copy of the array, not the array itself, and modifications to the array elements during the traversal process will not affect the copy (it feels like the copy is executed before the traversal). The array is copied once, and the array copy is operated during traversal). foreach has some side effects on array pointers. Unless it is reset, do not rely on the value of the array pointer during or after the foreach loop. As long as in foreach, you can directly take the elements in $array according to the key and perform various judgment and assignment operations.
The foreach syntax structure provides a simple way to traverse an array. 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 issued. There are two syntaxes:
foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement
The first format iterates over the given array_expression array. Each time through the loop, the value of the current cell is assigned to $value (a copy is created) and the pointer inside the array is moved forward one step (so the next cell will be obtained 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 each time through the loop (creating a copy).
Note:
When foreach starts executing, the pointer inside the array will automatically point to the first unit. This means there is no need to call reset() before the foreach loop.
Since foreach relies on an internal array pointer, modifying its value in a loop may cause unexpected behavior.
You can easily modify the elements of an array by prepending & before $value. This method assigns by reference rather than 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 ); // 最后取消掉引用 ?-->
Note: The $value reference of the last element of the array will remain after the foreach loop. It is recommended to use unset() to destroy it.