How to modify array value in php

coldplay.xixi
Release: 2023-03-05 10:22:01
Original
2720 people have browsed it

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);
Copy after login

②, 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);
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!