Home > Backend Development > PHP Problem > How to add key value to php two-dimensional array

How to add key value to php two-dimensional array

藏色散人
Release: 2023-03-13 21:26:01
Original
3672 people have browsed it

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.

How to add key value to php two-dimensional array

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'];
        }
Copy after login

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';
Copy after login

----------------------- -------------------------------------------------- ----------------------------------

$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;
Copy after login

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!

Related labels:
php
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