php Method to add key values to a two-dimensional array: 1. Create a PHP sample file; 2. Add the key value to the second dimensional array through the "foreach ($listening_collect as $key => $value){...}" method Dimensional arrays add key values.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to add key values to a two-dimensional array in php ?
php array loop to add key-value pairs
Two-dimensional array addition method:
Loop $listening_collect is a two-dimensional array, go to Add a you_answer element inside
$listening_collect = $this->ser_toefllistening->zt_collect($listening_articleid)->result_array(); foreach ($listening_collect as $key => $value){ //变量值$answer是个数组 answer是里面的一个元素 $answer = $this->ser_toefllistening->answer($value['id'],$u_id); $listening_collect[$key]['you_answer'] = $answer['answer']; }
Print the $listening_collect array
After adding, print the $listening_collect array
One-dimensional array addition method:
//这个是一维数组添加 foreach($arr as $key => &$vo){ $vo['name'] = '星星'; $vo['age'] = '24'; } $arr[$key]['name'] = '星星'; $arr[$key]['age'] = '24';
----------------------- -------------------------------------------------- ----------------------------------
$res = Db::name('ht_let_go') ->where("title", "like", "%$name%") ->order('id desc') ->paginate($size,false,['page'=> $page,'query'=>request()->param()])->toArray(); foreach ($res['data'] as $k => $v){ $res['data'][$k]['sum_num'] = Db::name('ht_let_go_buy')->where('let_go_id',$v['id'])->count(); } dump($res);die;
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to add key value to php two-dimensional array. For more information, please follow other related articles on the PHP Chinese website!