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

How to modify array value in php

coldplay.xixi
coldplay.xixiOriginal
2020-08-27 10:58:302776browse

php method to modify the array value: 1. Modify through statements, the code is [for($i = 0; $i < count(Array()); $i)]; 2. Two-dimensional array Implemented through the function foreach, the code is [foreach($users as &$user)].

How to modify array value in php

Related learning recommendations: php graphic tutorial

How to modify the array value in php:

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

example code in this form:

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

②, the two-dimensional array can also be passed foreach($users as &$user) Form modification

Example code

// 修改 二维数组中的 name为 Getchar
$users = array(
    array(‘name‘ => ‘GetcharZp‘, ‘age‘ => 19),
    array(‘name‘ => ‘Mcx‘, ‘age‘ => 18)
);
foreach ($users as &$user) {
    $user[‘name‘] = ‘Getchar‘;
}
print_r($users);

If you want to know more related learning, please pay attention to the php training column!

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