$v){$v = 1;}"."> How to modify the value in php foreach-PHP Problem-php.cn

How to modify the value in php foreach

藏色散人
Release: 2023-03-12 13:04:02
Original
2222 people have browsed it

php foreach method to modify the value: 1. Create a PHP sample file; 2. Modify it through "foreach($array as $k => $v){$v = 1;}" .

How to modify the value in php foreach

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to modify the value of php foreach?

The problem of using foreach to change the value of an array

Turn to the foreach page of the PHP document and it says:

"foreach The syntax structure provides a simple way to iterate over 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
Copy after login

The first format traverses the given array_expression array. In each loop, the value of the current unit is assigned to $value and the pointer inside the array moves forward one step (so the next unit will be obtained in the next loop).

The second format does the same thing, except that the key name of the current unit will also be assigned to the variable $key in each loop."

Then" The first format iterates over the given array_expression array. On each loop, the value of the current cell is assigned to $value and the pointer inside the array is moved forward one step (so the next cell will be obtained in the next loop)." Yes What's the meaning? This means that using foreach to traverse an array operates on a copy of the specified array, not the array itself. Just like having a clone of you, no matter how others punch or kick the clone of you, it will have no effect on you.

For example:

foreach($array as $k => $v){   $v = 1; }
Copy after login

Such a modification method does not modify $array itself, but modifies an array it copies. Although it is the same, it is not $array. Therefore, it has no impact on $array.

So what to do? To do this:

foreach($array as $k => $v){   $array[$k] = 1; }
Copy after login

Although $k and $v are also copied, the value of the copied $k is still the same as the value of $k in the original array, so this will succeed.

There is also a more advanced method: you can easily modify the elements of the array by adding & before $v. This method assigns by reference rather than copying a value. For example:

foreach($array as &$v){   $v = 1; } unset($v); // 最后取消掉引用
Copy after login

This will be successful.

Recommended learning: "PHP Video Tutorial"

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

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!