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

PHP single array simplified based on fields

WBOY
Release: 2016-08-04 09:20:57
Original
886 people have browsed it

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

<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>
Copy after login
Related labels:
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