首頁 >後端開發 >php教程 >php如何给关联数组添加新的key元素?

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

WBOY
WBOY原創
2016-06-23 13:10:281574瀏覽

$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