Home > Backend Development > PHP Problem > How to modify the contents of a two-dimensional array in php

How to modify the contents of a two-dimensional array in php

L
Release: 2023-03-01 09:36:02
Original
3378 people have browsed it

How to modify the contents of a two-dimensional array in php

PHP Modify the values ​​in the two-dimensional array

1. The two-dimensional array can be passed through for($i = 0; $i < count(Array()); $i) This form modifies the

example code:

// 修改 二维数组中的 name为 Getchar
$users = array(    
    array(&#39;name&#39; => &#39;GetcharZp&#39;, &#39;age&#39; => 19),    
    array(&#39;name&#39; => &#39;Mcx&#39;, &#39;age&#39; => 18)
);
for($i = 0; $i < count(Array()); ++ $i)  {  
    $users[$i][&#39;name&#39;] = &#39;Getchar&#39;;}
print_r($users);
Copy after login

2. Two-dimensional arrays can also be passed in the form of foreach($users as &$user) Modify

Instance code

// 修改 二维数组中的 name为 Getchar
$users = array(   
array(&#39;name&#39; => &#39;GetcharZp&#39;, &#39;age&#39; => 19),    
array(&#39;name&#39; => &#39;Mcx&#39;, &#39;age&#39; => 18) 
);
foreach ($users as &$user) {    
$user[&#39;name&#39;] = &#39;Getchar&#39;; 
}unset($user); // 销毁掉 user 引用print_r($users);
Copy after login

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of How to modify the contents of a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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 Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template