Heim >Backend-Entwicklung >PHP-Tutorial >数组复建
数组重构
Array<br />(<br /> [0] => Array<br /> (<br /> [title] => 111<br /> [name] => 上海<br /> )<br /><br /> [1] => Array<br /> (<br /> [title] => 111<br /> [name] => 江苏<br /> )<br /><br /> [2] => Array<br /> (<br /> [title] => dd<br /> [name] => 上海<br /> )<br /><br /> [3] => Array<br /> (<br /> [title] => dd<br /> [name] => 江苏<br /> )<br /><br /> [4] => Array<br /> (<br /> [title] => dd<br /> [name] => 浙江<br /> )<br /><br />)
<br />array(<br /> [0]=>array(<br /> [title]=111<br /> [name]=上海,苏州<br />),<br />);<br />
$arr = array(<br /> array('title'=>'111','name'=>'上海'),<br /> array('title'=>'111','name'=>'杭州'),<br /> array('title'=>'dd','name'=>'上海'),<br /> array('title'=>'dd','name'=>'宁波'),<br /> array('title'=>'dd','name'=>'无锡'),<br /> );<br />$title_arr = array();<br />$data = array();<br />foreach($arr as $value){<br /> if(!in_array($value['title'],$title_arr)){<br /> array_push($title_arr,$value['title']);<br /> $data[] = $value;<br /> }else{<br /> $index = array_search($value['title'],$title_arr);<br /> $data[$index]['name'] .= ','.$value['name'];<br /> }<br />}<br />var_dump($data);
<?php<br />$arr = array(<br /> array('title'=>'111','name'=>'上海'),<br /> array('title'=>'111','name'=>'杭州'),<br /> array('title'=>'dd','name'=>'上海'),<br /> array('title'=>'dd','name'=>'宁波'),<br /> array('title'=>'dd','name'=>'无锡'),<br /> );<br />$tmp = $arr[0]['title'];<br />$newarr = array();<br />foreach($arr as $value){<br /> $key = $tmp == $value['title'] ? $tmp : $value['title'];<br /> $newarr[$key]['title'] = $value['title'];<br /> $newarr[$key]['name'] .= $value['name'].','; <br />}<br />var_dump($newarr);
select a.title, GROUP_CONCAT(c.name) AS name<br /> from information_agent as a <br /> left join information_agent_area as b on a.id=b.tid<br /> left join sales_area as c on b.area_id=c.id<br /> GROUP BY a.title<br />
<br />基本查询 <br />mysql> select * from aa;<br />+------+------+<br /><br><font color='#FF8000'>------解决思路----------------------</font><br> id<br><font color='#FF8000'>------解决思路----------------------</font><br> name <br><font color='#FF8000'>------解决思路----------------------</font><br><br />+------+------+<br /><br><font color='#FF8000'>------解决思路----------------------</font><br>1 <br><font color='#FF8000'>------解决思路----------------------</font><br> 10<br><font color='#FF8000'>------解决思路----------------------</font><br><br /><br><font color='#FF8000'>------解决思路----------------------</font><br>1 <br><font color='#FF8000'>------解决思路----------------------</font><br> 20<br><font color='#FF8000'>------解决思路----------------------</font><br><br /><br><font color='#FF8000'>------解决思路----------------------</font><br>1 <br><font color='#FF8000'>------解决思路----------------------</font><br> 20<br><font color='#FF8000'>------解决思路----------------------</font><br><br /><br><font color='#FF8000'>------解决思路----------------------</font><br>2 <br><font color='#FF8000'>------解决思路----------------------</font><br> 20<br><font color='#FF8000'>------解决思路----------------------</font><br><br /><br><font color='#FF8000'>------解决思路----------------------</font><br>3 <br><font color='#FF8000'>------解决思路----------------------</font><br> 200 <br><font color='#FF8000'>------解决思路----------------------</font><br><br /><br><font color='#FF8000'>------解决思路----------------------</font><br>3 <br><font color='#FF8000'>------解决思路----------------------</font><br> 500 <br><font color='#FF8000'>------解决思路----------------------</font><br><br />+------+------+<br />6 rows in set (0.00 sec)<br /><br />1.以id分组,把name字段的值打印在一行,逗号分隔(默认) <br />mysql> select id,group_concat(name) from aa group by id;<br />+------+--------------------+<br /><br><font color='#FF8000'>------解决思路----------------------</font><br> id<br><font color='#FF8000'>------解决思路----------------------</font><br> group_concat(name) <br><font color='#FF8000'>------解决思路----------------------</font><br><br />+------+--------------------+<br /><br><font color='#FF8000'>------解决思路----------------------</font><br>1 <br><font color='#FF8000'>------解决思路----------------------</font><br> 10,20,20<br><font color='#FF8000'>------解决思路----------------------</font><br><br /><br><font color='#FF8000'>------解决思路----------------------</font><br>2 <br><font color='#FF8000'>------解决思路----------------------</font><br> 20 <br><font color='#FF8000'>------解决思路----------------------</font><br><br /><br><font color='#FF8000'>------解决思路----------------------</font><br>3 <br><font color='#FF8000'>------解决思路----------------------</font><br> 200,500<br><font color='#FF8000'>------解决思路----------------------</font><br><br />+------+--------------------+<br />3 rows in set (0.00 sec)<br />