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

一、Iterator和foreach的区别多态差别(foreach底层就是Iterator)Iterator是一个接口类型,他不关心集合或者数组的类型;for和foreach都需要先知道集合的类型,甚至是集合内元素的类型;1.为啥说foreach底层就是Iterator编写的代码:反编译代码:二、foreach与iterator时remove的区别先来看阿里java开发手册但1的时候不会报错,2的时候就会报错(java.util.ConcurrentModificationException)首

php判断foreach循环到第几个的步骤:1、创建一个“$fruits”的数组;2、创建一个计数器变量“$counter”初始值为0;3、使用“foreach”循环遍历数组,并在循环体中增加计数器变量的值,再输出每个元素和它们的索引;4、在“foreach”循环体外输出计数器变量的值,以确认循环到了第几个元素。

这篇文章将为大家详细讲解有关PHP返回一个键值翻转后的数组,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。PHP键值翻转数组键值翻转是一种对数组进行的操作,它将数组中的键和值进行交换,生成一个新的数组,其中原始键作为值,原始值作为键。实现方法在php中,可以通过以下方法对数组进行键值翻转:array_flip()函数:array_flip()函数专门用于键值翻转操作。它接收一个数组作为参数,并返回一个新的数组,其中键和值已交换。$original_array=[

这篇文章将为大家详细讲解有关PHP返回数组中的当前元素,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。获取PHP数组中的当前元素php为访问和操作数组提供了多种方法,其中包括获取数组中的当前元素。以下介绍几种常用的技术:1.current()函数current()函数返回数组内部指针当前指向的元素。指针最初指向数组的第一个元素。使用以下语法:$currentElement=current($array);2.key()函数key()函数返回数组内部指针当前指向元

如何使用forEach函数遍历对象的属性?在JavaScript中,我们经常需要对对象的属性进行遍历操作。如果你想使用一种简洁的方法来遍历对象的属性,forEach函数是一个非常好的选择。在本文中,我们将介绍如何使用forEach函数来遍历对象的属性,并提供具体的代码示例。首先,让我们来了解一下forEach函数的基本用法。forEach函数是Java

区别:1、for通过索引来循环遍历每一个数据元素,而forEach通过JS底层程序来循环遍历数组的数据元素;2、for可以通过break关键词来终止循环的执行,而forEach不可以;3、for可以通过控制循环变量的数值来控制循环的执行,而forEach不行;4、for在循环外可以调用循环变量,而forEach在循环外不能调用循环变量;5、for的执行效率要高于forEach。

在PHP中,foreach语句被广泛用于对数组和对象进行遍历。在进行循环的过程中,我们有时需要获取当前循环到第几个元素。本文将为大家介绍怎样在PHP foreach循环中获取当前元素的索引值。

PHP中break关键字的作用和用法详解在PHP编程中,break是一种控制流语句,用于中断当前循环或switch语句,并跳出该循环或switch。本文将详细介绍break关键字的作用和用法。一、循环中的break在循环结构中,break的作用是提前终止循环,跳出循环体执行循环后的代码。常见的循环结构有for、while和do...while。在for循环中


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
