Array( [0] => Array ( [title] => 111 [name] => 上海 ) [1] => Array ( [title] => 111 [name] => 江苏 ) [2] => Array ( [title] => dd [name] => 上海 ) [3] => Array ( [title] => dd [name] => 江苏 ) [4] => Array ( [title] => dd [name] => 浙江 ))
array( [0]=>array( [title]=111 [name]=上海,苏州),);
$arr = array( array('title'=>'111','name'=>'上海'), array('title'=>'111','name'=>'杭州'), array('title'=>'dd','name'=>'上海'), array('title'=>'dd','name'=>'宁波'), array('title'=>'dd','name'=>'无锡'), );$title_arr = array();$data = array();foreach($arr as $value){ if(!in_array($value['title'],$title_arr)){ array_push($title_arr,$value['title']); $data[] = $value; }else{ $index = array_search($value['title'],$title_arr); $data[$index]['name'] .= ','.$value['name']; }}var_dump($data);
这是一个很普遍的需求,所以写一个通用函数
起了一个长长的名字,来提醒你们本可以在 MySQL 中就能完成的(group_concat)
function array_group_concat($ar, $field, $group_by) { $res = array(); foreach($ar as $row) { $g = $row[$group_by]; if(isset($res[$g])) $res[$g][$field] .= ',' . $row[$field]; else $res[$g] = $row; } return array_values($res);}
$arr = array( array('title'=>'111','name'=>'上海'), array('title'=>'111','name'=>'杭州'), array('title'=>'dd','name'=>'上海'), array('title'=>'dd','name'=>'宁波'), array('title'=>'dd','name'=>'无锡'), );print_r(array_group_concat($arr, 'name', 'title'));
Array( [0] => Array ( [title] => 111 [name] => 上海,杭州 ) [1] => Array ( [title] => dd [name] => 上海,宁波,无锡 ))
$arr = array( array('title'=>'111','name'=>'上海'), array('title'=>'111','name'=>'杭州'), array('title'=>'dd','name'=>'上海'), array('title'=>'dd','name'=>'宁波'), array('title'=>'dd','name'=>'无锡'), );print_r(array_group_concat($arr, 'title', 'name'));
Array( [0] => Array ( [title] => 111,dd [name] => 上海 ) [1] => Array ( [title] => 111 [name] => 杭州 ) [2] => Array ( [title] => dd [name] => 宁波 ) [3] => Array ( [title] => dd [name] => 无锡 ))
<?php$arr = array( array('title'=>'111','name'=>'上海'), array('title'=>'111','name'=>'杭州'), array('title'=>'dd','name'=>'上海'), array('title'=>'dd','name'=>'宁波'), array('title'=>'dd','name'=>'无锡'), );$tmp = $arr[0]['title'];$newarr = array();foreach($arr as $value){ $key = $tmp == $value['title'] ? $tmp : $value['title']; $newarr[$key]['title'] = $value['title']; $newarr[$key]['name'] .= $value['name'].','; }var_dump($newarr);
这个看不是很难,根据数组title来做判断。
重新定义一个接收数组。
循环原数组
原来数组title的值相同,把title赋值到新数组中name值做拼接;
不相同,title和name都赋值过去。
循环完后新数组就是你要的格式
这是一个很普遍的需求,所以写一个通用函数
起了一个长长的名字,来提醒你们本可以在 MySQL 中就能完成的(group_concat)
function array_group_concat($ar, $field, $group_by) { $res = array(); foreach($ar as $row) { $g = $row[$group_by]; if(isset($res[$g])) $res[$g][$field] .= ',' . $row[$field]; else $res[$g] = $row; } return array_values($res);}
$arr = array( array('title'=>'111','name'=>'上海'), array('title'=>'111','name'=>'杭州'), array('title'=>'dd','name'=>'上海'), array('title'=>'dd','name'=>'宁波'), array('title'=>'dd','name'=>'无锡'), );print_r(array_group_concat($arr, 'name', 'title'));
Array( [0] => Array ( [title] => 111 [name] => 上海,杭州 ) [1] => Array ( [title] => dd [name] => 上海,宁波,无锡 ))
$arr = array( array('title'=>'111','name'=>'上海'), array('title'=>'111','name'=>'杭州'), array('title'=>'dd','name'=>'上海'), array('title'=>'dd','name'=>'宁波'), array('title'=>'dd','name'=>'无锡'), );print_r(array_group_concat($arr, 'title', 'name'));
Array( [0] => Array ( [title] => 111,dd [name] => 上海 ) [1] => Array ( [title] => 111 [name] => 杭州 ) [2] => Array ( [title] => dd [name] => 宁波 ) [3] => Array ( [title] => dd [name] => 无锡 ))
$sql="select a.title,c.name from information_agent as a left join information_agent_area as b on a.id=b.tid left join sales_area as c on b.area_id=c.id";
select a.title, GROUP_CONCAT(c.name) AS name from information_agent as a left join information_agent_area as b on a.id=b.tid left join sales_area as c on b.area_id=c.id GROUP BY a.title
select a.title, GROUP_CONCAT(c.name) AS name from information_agent as a left join information_agent_area as b on a.id=b.tid left join sales_area as c on b.area_id=c.id GROUP BY a.title
基本查询 mysql> select * from aa;+------+------+| id| name |+------+------+|1 | 10||1 | 20||1 | 20||2 | 20||3 | 200 ||3 | 500 |+------+------+6 rows in set (0.00 sec)1.以id分组,把name字段的值打印在一行,逗号分隔(默认) mysql> select id,group_concat(name) from aa group by id;+------+--------------------+| id| group_concat(name) |+------+--------------------+|1 | 10,20,20||2 | 20 ||3 | 200,500|+------+--------------------+3 rows in set (0.00 sec)
$arr = array( array('title'=>'111','name'=>'上海'), array('title'=>'111','name'=>'杭州'), array('title'=>'dd','name'=>'上海'), array('title'=>'dd','name'=>'宁波'), array('title'=>'dd','name'=>'无锡'), );$title_arr = array();$data = array();foreach($arr as $value){ if(!in_array($value['title'],$title_arr)){ array_push($title_arr,$value['title']); $data[] = $value; }else{ $index = array_search($value['title'],$title_arr); $data[$index]['name'] .= ','.$value['name']; }}var_dump($data);
这个看不是很难,根据数组title来做判断。
重新定义一个接收数组。
循环原数组
原来数组title的值相同,把title赋值到新数组中name值做拼接;
不相同,title和name都赋值过去。
循环完后新数组就是你要的格式
<?php$arr = array( array('title'=>'111','name'=>'上海'), array('title'=>'111','name'=>'杭州'), array('title'=>'dd','name'=>'上海'), array('title'=>'dd','name'=>'宁波'), array('title'=>'dd','name'=>'无锡'), );$tmp = $arr[0]['title'];$newarr = array();foreach($arr as $value){ $key = $tmp == $value['title'] ? $tmp : $value['title']; $newarr[$key]['title'] = $value['title']; $newarr[$key]['name'] .= $value['name'].','; }var_dump($newarr);