php 二维数组遍历赋值
我个人在项目中的写法:
//遍历二维数组foreach($tmp_array as $key => $value){ //动态生成图片的URL $attach_url = $this->qiniu_utils->get_download_url($tmp_array[$key][‘attach_save_name‘] , ‘7vih5p.com1.z0.glb.clouddn.com‘, ‘PRIVATE‘); //赋值URL $tmp_array[$key][‘attach_save_url‘] = $attach_url;}$result[‘feed_img_ids‘] = $tmp_array; --------------------------------------------------------------------------- 以下是网上的参考答案
Q:
$arr = array(‘one‘=>array(‘name‘=>‘张三‘,‘age‘=>‘23‘,‘sex‘=>‘男‘), ‘two‘=>array(‘name‘=>‘李四‘,‘age‘=>‘43‘,‘sex‘=>‘女‘), ‘three‘=>array(‘name‘=>‘王五‘,‘age‘=>‘32‘,‘sex‘=>‘男‘), ‘four‘=>array(‘name‘=>‘赵六‘,‘age‘=>‘12‘,‘sex‘=>‘女‘)); foreach($arr as $key=>$value){ foreach($value as $key2=>$value2){ $arr[$ey][$value][$key2][‘age‘]= 55; }} print_r($arr); 怎么给age赋一个新值? A:
foreach($arr as $key=>$value){ $arr[$key][‘age‘]=55}看你的需求 这就行了 参考2:
php遍历数组赋值为什么不行啊?
http://www.oschina.net/question/1434721_149652?sort=default&p=2#answers
时间: 2024-12-17 16:41:55