$v){$v = 1;}"."/> $v){$v = 1;}".">
search
HomeBackend DevelopmentPHP ProblemHow to use foreach to modify an array in php

php foreach method to modify the array: first create a PHP sample file; then modify the specified array through the foreach statement "foreach($array as $k => $v){$v = 1;}" The value is enough.

How to use foreach to modify an array in php

Recommended: "PHP Video Tutorial"

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

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

"The foreach syntax structure provides a simple way to traverse the array. foreach can only be applied to arrays and objects. If you try to apply it to other data types Variables, or uninitialized variables will issue an error message. 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. In each loop, the value of the current cell is assigned to $value and The pointer inside the array moves forward one step (so the next element will be obtained in the next loop).

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

Then "The first format traverses the given array_expression array. In each loop, the value of the current unit is assigned to $value and the array The internal pointer moves forward one step (so the next unit in the loop will be obtained)." What does this 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 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;
}

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;
}

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); // 最后取消掉引用

This will be successful.

The above is the detailed content of How to use foreach to modify an array in php. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use