Home  >  Article  >  Backend Development  >  How to modify the value in an array in php

How to modify the value in an array in php

王林
王林Original
2020-08-04 15:37:283216browse

How to modify the values ​​in the array in php: You can use a for loop to modify, such as [for($i = 0; $i < count(Array()); $i)]. It can also be modified through a foreach loop, such as [foreach($users as &$user)].

How to modify the value in an array in php

The two-dimensional array can be modified through for($i = 0; $i < count(Array()); $i).

(Recommended tutorial: php graphic tutorial)

Code implementation:

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

Two-dimensional arrays can also be passed through foreach($users as &$user) modified.

(Video tutorial recommendation: php video tutorial)

Code implementation:

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

The above is the detailed content of How to modify the value in 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