Home >Backend Development >PHP Tutorial >PHP single array simplified based on fields

PHP single array simplified based on fields

WBOY
WBOYOriginal
2016-08-04 09:20:57887browse

PHP single array simplified based on fields
This is my watch

How to get
PHP single array simplified based on fields
such an array? Thank you

Reply content:

PHP single array simplified based on fields
This is my watch

How to get
PHP single array simplified based on fields
such an array? Thank you

There is a group_concat function in Mysql, you can try it.
The approximate sql statement is as follows:

<code>SELECT term_id, group_concat(village_name) as area FROM tb_name GROUP BY term_id;</code>

<code>$data=array();
        foreach(数据库数组 as  $v){
            if(!isset($data[$v['team_id']])){
                $data[$v['team_id']]=$v['village_name'];
            }else{
                $data[$v['team_id']].=','.$v['village_name'];
            }
        }
        $d=array();
        foreach($data as $kk=>$vv){
            $d[]=array(
                'team_id'=>$kk,
                'area'=>$vv,
            );
        }
        unset($data);
        var_dump($d);
</code>
Statement:
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