数组重构

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-20 12:55:14
Original
2238 people have browsed it

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] => 浙江        ))
Copy after login


我想把 上面的 二位数组的格式 重构为 title名字一样的合并起来 例如
array(   [0]=>array(       [title]=111       [name]=上海,苏州),);
Copy after login


回复讨论(解决方案)

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

这是一个很普遍的需求,所以写一个通用函数
起了一个长长的名字,来提醒你们本可以在 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);}
Copy after login
Copy after login

例:
按 title 分组取 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, 'name', 'title'));
Copy after login
Copy after login
Array(    [0] => Array        (            [title] => 111            [name] => 上海,杭州        )    [1] => Array        (            [title] => dd            [name] => 上海,宁波,无锡        ))
Copy after login
Copy after login

按 name 分组取 title
$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'));
Copy after login
Copy after login
Array(    [0] => Array        (            [title] => 111,dd            [name] => 上海        )    [1] => Array        (            [title] => 111            [name] => 杭州        )    [2] => Array        (            [title] => dd            [name] => 宁波        )    [3] => Array        (            [title] => dd            [name] => 无锡        ))
Copy after login
Copy after login

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

这个看不是很难,根据数组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);}
Copy after login
Copy after login

例:
按 title 分组取 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, 'name', 'title'));
Copy after login
Copy after login
Array(    [0] => Array        (            [title] => 111            [name] => 上海,杭州        )    [1] => Array        (            [title] => dd            [name] => 上海,宁波,无锡        ))
Copy after login
Copy after login

按 name 分组取 title
$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'));
Copy after login
Copy after login
Array(    [0] => Array        (            [title] => 111,dd            [name] => 上海        )    [1] => Array        (            [title] => 111            [name] => 杭州        )    [2] => Array        (            [title] => dd            [name] => 宁波        )    [3] => Array        (            [title] => dd            [name] => 无锡        ))
Copy after login
Copy after login



版主 你好, 我的这个结果 是通过 下面这条语句得到的 在mysql段 利用group_concat 该如何修改
$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";
Copy after login

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

指令串拆行看的清楚些,注意有大写的那些地方

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

指令串拆行看的清楚些,注意有大写的那些地方



嗯嗯 ,好的 谢谢指导, 可以实现了。

基本查询 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)
Copy after login

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


谢谢

这个看不是很难,根据数组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);
Copy after login
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