&$value){//Loop body statement block;}"."/> &$value){//Loop body statement block;}".">

Home  >  Article  >  Backend Development  >  How to modify the original array in php in foreach loop

How to modify the original array in php in foreach loop

青灯夜游
青灯夜游Original
2022-09-28 17:18:083178browse

In PHP, you only need the foreach statement to use a reference loop, then operating (modifying) the original array in the loop body will affect the original array itself; the syntax "foreach ($array as &$value ){//Loop body statement block;}" or "foreach ($array as $key => &$value){//Loop body statement block;}".

How to modify the original array in php in foreach loop

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

foreach is a statement specially designed for traversing arrays , is a commonly used method when traversing arrays, and provides great convenience in traversing arrays; after PHP5, objects can also be traversed (foreach can only be applied to arrays and objects).

The foreach statement traverses the array regardless of the array subscript, and can be used for discontinuous index arrays and associative arrays with strings as subscripts.

The foreach statement has two syntax formats:

Grammar format 1:

foreach ($array as $value){
    //循环体语句块;
}
  • Traverse the given $array array and assign the value of the current array to $value in each loop.

Syntax format 2:

foreach ($array as $key => $value){
    //循环体语句块;
}
  • Traverse the given $array array, and in each loop it will The value of the current array is assigned to $value, and the key name is assigned to $key.

Explanation:

When the foreach statement loops, the pointer inside the array will move forward one step, so that the next array element will be obtained in the next loop. Stop traversing and exit the loop until it reaches the end of the array.

Modify the original array in the foreach loop

Generally, when using the foreach statement to traverse the array, the backup of the array is operated. Generally does not affect the array itself.

For example: modify the array inside the loop, and then access the array outside the loop, you will find that the array has not changed

<?php
header("Content-type:text/html;charset=utf-8");
$array= array(1,2,3,4,5,);
echo "原数组:";
var_dump($array);
foreach ($array as $value) { 
    $value = $value*2;  // 元素值乘以2
}
echo "foreach处理后的数组:";
var_dump($array);
?>

How to modify the original array in php in foreach loop

It can be seen that the two outputs are Similarly, the array elements have not changed; at this time, modifications to the original array in the foreach loop are invalid.

But sometimes it is necessary to change the array through a foreach loop, what should we do?

You can use a reference cycle (that is, add & before $value, so that the foreach statement will assign a value by reference instead of copying a value), then Operations on the original array within the loop body will affect the original array itself.

<?php
header("Content-type:text/html;charset=utf-8");
$array= array(1,2,3,4,5,);
echo "原数组:";
var_dump($array);
foreach ($array as &$value) { 
    $value = $value*2;  // 元素值乘以2
}
echo "foreach处理后的数组:";
var_dump($array);
?>

How to modify the original array in php in foreach loop

It can be seen that the array elements have changed, and the element values ​​have been multiplied by 2.

But before the last element, there is an extra &. What's wrong with this? How to remove it?

This is because the $value reference of the last element of the array will remain after the foreach loop. We need to use unset() to destroy the array before outputting it.

unset($value); // 最后取消掉引用
var_dump($array)

How to modify the original array in php in foreach loop

& The symbol disappeared!

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to modify the original array in php in foreach loop. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn