How to modify the values ​​in a two-dimensional array in php?

coldplay.xixi
Release: 2023-03-03 09:16:02
Original
4377 people have browsed it

php method to modify the value in a two-dimensional array: 1. Modify through the [for($i = 0; $i < count(Array()); $i)] statement; 2. Modify through foreach( $users as &$user) statement modification.

How to modify the values ​​in a two-dimensional array in php?

php method to modify the value in the two-dimensional array:

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

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)
);
for ($i = 0; $i < count($users); ++ $i) {
    $users[$i][&#39;name&#39;] = &#39;Getchar&#39;;
}
print_r($users);
Copy after login

2. Two-dimensional array also You can modify the example code in the form of

foreach($users as &$user)

// 修改 二维数组中的 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

Related learning recommendations: Getting Started with PHP Programming To master

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template