Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use php foreach to modify values

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to use php foreach to modify the value, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer

How does php foreach modify the value?

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

Turn to the foreach page of the PHP document and write:

"the foreach syntax structure provides an easy way to traverse arrays. Foreach can only be applied to arrays and objects, and if you try to apply to variables of other data types, or uninitialized variables will issue an error message. There are two syntaxes:

Foreach (array_expression as $value) statementforeach (array_expression as $key = > $value) statement

The first format iterates through a 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 (so you will get the next cell in the next loop).

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

So "the first format iterates through a given array_expression array. In each loop, the value of the current cell is assigned to $value and the pointer inside the array moves forward (so the next cell will be obtained in the next loop)." What does it mean? 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 cloned you, no matter how much others punch and kick you, it will have no effect on you.

For example:

Foreach ($array as $k = > $v) {$v = 1;}

This modification method modifies not $array itself, but an array that modifies its copy, the same but not $array. So it has no effect on $array.

So how does this work? To do this:

Foreach ($array as $k = > $v) {$array [$k] = 1;}

Although $k is copied as well as $v, the value of the copied $k is the same as that of the original array, so it can be successful.

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

Foreach ($array as & $v) {$v = 1;} unset ($v); / finally cancel the reference on how to use php foreach to modify the value of the answer to share here, I hope the above content can be of some help to you, if you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report