


Traversal replacement steps: 1. Use the foreach statement to traverse the array by referencing the loop, the syntax is "foreach($arr as $k=>&$v){//Loop body statement block;}"; 2. In the loop body, use the "===" operator to find the elements of the specified field and reassign the value. The syntax "foreach($arr as $k=>&$v){if($k===specifies Field value){$v=replacement value;}}".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
In the php array, you can use the foreach statement to Traverse and replace the value of a certain field (key name).
Implementation steps:
Step 1: Use the foreach statement to traverse the array through reference loops
foreach ($array as $key => &$value){ //循环体语句块; }
Traverse the given The $array array will assign the value of the current array to $value and the key name to $key in each loop.
Generally, when using the foreach statement to traverse an array, it operates on the backup of the array and generally does not affect the array itself.
If you want to change the array through a loop, you can use a reference loop (add
&
before$value
, so that the foreach statement will assign a value by reference instead of copying a value ), then operating on the array within the loop body will affect the array itself.
Step 2: In the loop body, use the "===" operator to find the specified field and reassign the value
if($key===指定字段){ $value=新值; }
Complete example Code:
<?php header(&#39;content-type:text/html;charset=utf-8&#39;); function f($arr,$s,$r){ foreach($arr as $k=>&$v){ if($k===$s){ $v=$r; } } echo "字段 $s 的值替换为 $r"; var_dump($arr); } $arr=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); echo "原数组:"; var_dump($arr); f($arr,"a","aaa"); f($arr,"b","bbb"); f($arr,"c","ccc"); ?>
It can be seen that there is an & before the last element. That is because the $value reference of the last element of the array is after the foreach loop. Will remain. We need to use unset() to destroy it.
unset($v); // 最后取消掉引用
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to traverse and replace the value of a certain field (key name) in a php array. For more information, please follow other related articles on the PHP Chinese website!

求和方法:1、用array_column()获取多维数组中指定一列的全部元素,语法“rray_column(数组, '指定列名')”,会返回一个包含全部元素的结果数组;2、用“array_sum(结果数组)”计算结果数组中所有元素的和即可。

增加元素的方法:1、使用array_push()函数,语法“array_push(二维数组,值1,值2...);”;2、使用array_splice()函数,语法“array_splice(二维数组,count(二维数组),0,元素值)”。

获取方法:1、用“array_values(数组)”将指定数组转为索引数组;2、用“array_search(数值,索引数组)”,在索引数组中搜索数值,返回对应的索引值(下标);3、用“索引值+1”语句获取元素在数组中的位置值。

php数组里面可以放数组。PHP数组可以存储所有类型的数据,当然也包括数组本身;如果一个数组中的元素是另一个数组,就构成了包含数组的数组,即多维度数组。数组的不同维度标志着需用几个下标(索引)来获取对应的数组元素,比如二维数组需用两个下标。

方法:1、循环遍历数组,语法“foreach($arr as $k=>$v){}”;2、循环体中,用“==”判断元素值是否为null,如果是则用unset()删除该元素,语法“if($v==null){unset($arr[$k]);}”。

php中只比较值的数组交集函数是“array_intersect()”;该函数用于比较两个(或更多个)数组的键值,语法“array_intersect(数组1,数组2...)”,会返回一个交集数组,所包含的值是从被比较的数组(数组1)中取。

去掉方法:1、用“sort($arr)”对数组进行升序排序,排序后该数组的第一个元素就是最小值,最后一个元素就是最大值;2、用“array_pop($arr)”删除最后一个元素,用“array_shift($arr)”删除第一个元素即可。

php中可用preg_match_all()配合正则表达式过滤字符串,只获取中文字符;语法“preg_match_all("/[\x{4e00}-\x{9fff}]+/u","$str",$arr);”,会将匹配字符存入“$arr”数组中。


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
