PHP is a powerful programming language that provides many convenient and easy-to-use array processing functions, of which foreach
loop is one of them.
In PHP, foreach
Loops can be used to iterate through all elements in an array and operate on them. If you want to modify the elements in the array, foreach
loop is a more convenient method. The following will introduce how the foreach
loop modifies the array.
foreach
The basic syntax of the loop is as follows:
foreach($array as $key => $value){ // 对元素进行操作 }
Among them, $array
represents the array to be traversed, $key
represents the key of the current element, $value
represents the value of the current element. foreach
The loop will traverse all the elements in the array and perform the operation in the loop body once for each element.
To modify the array elements, just operate on the elements in the loop body. For example, suppose there is an associative array $arr
whose elements are 'name' => 'Tom', 'age' => 20, 'gender' => 'male'
, we want to change the value of the 'age'
element to 25, we can use the foreach
loop to achieve this:
$arr = ['name' => 'Tom', 'age' => 20, 'gender' => 'male']; foreach($arr as $key => $value){ if($key == 'age'){ $arr[$key] = 25; } } print_r($arr);
The output result is Array ([name] => Tom [age] => 25 [gender] => male )
. It can be seen that the foreach
loop can easily modify the elements in the array.
If you want to modify all elements, just operate on $value
in the loop body. For example, suppose there is an index array $arr
whose elements are [1, 2, 3, 4, 5]
, and we want to multiply the values of all elements in it by 2, You can use a foreach
loop to achieve this:
$arr = [1, 2, 3, 4, 5]; foreach($arr as $key => $value){ $arr[$key] = $value * 2; } print_r($arr);
The output result is Array ( [0] => 2 [1] => 4 [2] => 6 [ 3] => 8 [4] => 10 )
. As you can see, the foreach
loop easily completes the modification of all elements in the array.
If you only want to modify some elements in the array, you can use the if
statement to determine the elements to be modified. For example, suppose there is an associative array $arr
whose elements are 'name' => 'Tom', 'age' => 20, 'gender' => 'male'
, we want to change the value of the 'age'
element to 25, and only modify the 'age'
element. We can use the if
statement to achieve this:
$arr = ['name' => 'Tom', 'age' => 20, 'gender' => 'male']; foreach($arr as $key => $value){ if($key == 'age'){ $arr[$key] = 25; } } print_r($arr);
The output result is Array ( [name] => Tom [age] => 25 [gender] => male )
. It can be seen that the foreach
loop uses the if
statement to determine the elements to be modified, and realizes the modification of some elements.
When using foreach
to modify the array in a loop, you need to pay attention to the following points:
For example, suppose there is an associative array $arr
whose elements are 'name' => 'Tom', 'age' => 20, ' gender' => 'male'
, if you use the following code to modify the value of the 'age'
element, it will not actually take effect:
foreach($arr as $key => $value){ if($key == 'age'){ $value = 25; } }
because $value
is a copied value, modifying it will not affect the original array. If you want to modify the original array, you must use reference transfer, that is, add the &
symbol before the loop variable:
foreach($arr as $key => &$value){ if($key == 'age'){ $value = 25; } }
At this time, modifying the value of $value
will take effect , and will affect the original array.
foreach
Looping is a convenient way to traverse an array and can also be used to modify elements in an array. To modify an array element, just operate on the corresponding element in the loop body. If you want to modify all elements, you only need to operate $value
; if you only need to modify some elements, you can use the if
statement to judge.
When using foreach
to modify an array in a loop, you need to pay attention to reference passing and it is not recommended to add or delete elements in the loop. Mastering the skills of using foreach
loops can make it easier to process arrays.
The above is the detailed content of How to modify the array in php foreach loop. For more information, please follow other related articles on the PHP Chinese website!