Home >Backend Development >PHP Tutorial >How to modify the value of an array in php?

How to modify the value of an array in php?

烟雨青岚
烟雨青岚Original
2020-06-13 11:03:094224browse

How to modify the value of an array in php?

#How to modify the value of an array in php?

How to modify the value in an array in PHP:

1. The array can be passed through for($i = 0; $i < count (Array()); $i) This kind of statement modification.

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

2. The array can also be modified through foreach($users as &$user).

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)
);
foreach ($users as &$user) {
    $user[&#39;name&#39;] = &#39;Getchar&#39;;
}unset($user); // 销毁掉 user 引用
print_r($users);

Recommended tutorial: "php tutorial"

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