首页 >后端开发 >php教程 >php如何给关联数组添加新的key元素?

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

WBOY
WBOY原创
2016-06-23 13:10:281548浏览

$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");


回复讨论(解决方案)

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

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



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);

$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);

$v[] = 'xx';

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn