Home > Backend Development > PHP Tutorial > php如何给关联数组添加新的key元素?

php如何给关联数组添加新的key元素?

WBOY
Release: 2016-06-23 13:10:28
Original
1547 people have browsed it

$a = array("0"=>array("id"=>332,"name"=>"myname"),"1"=>array("id"=>333,"name"=>"yourname");如何变为下面的格式?$a = array("0"=>array("id"=>332,"name"=>"myname","district"=>"mydistrict"),"1"=>array("id"=>333,"name"=>"yourname","district"=>"testdistrict");
Copy after login


回复讨论(解决方案)

你要添加的值是固定的 还是随机的 还是你要设置好的?

你要添加的值是固定的 还是随机的 还是你要设置好的?



key是固定的,比如:district是固定的,但是它的值是动态从数据库中取出来赋值的。

$a = array("0"=>array("id"=>332,"name"=>"myname"),"1"=>array("id"=>333,"name"=>"yourname");foreach($a as &$v){    $v['district'] = 'xxx';}unset($v);
Copy after login

$a = array("0"=>array("id"=>332,"name"=>"myname"),"1"=>array("id"=>333,"name"=>"yourname");foreach($a as $k=>$v){    $a[$k]['district'] = 'mydistrict';}print_r($a);
Copy after login

$v[] = 'xx';
Copy after login

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