数组复建

WBOY
Release: 2016-06-13 12:21:13
Original
976 people have browsed it

数组重构

本帖最后由 memory_qian 于 2015-06-03 13:16:30 编辑
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 />)
Copy after login


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

------解决思路----------------------
$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);
Copy after login

------解决思路----------------------
<?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);
Copy after login

------解决思路----------------------
这个看不是很难,根据数组title来做判断。
重新定义一个接收数组。
循环原数组
原来数组title的值相同,把title赋值到新数组中name值做拼接;
不相同,title和name都赋值过去。
循环完后新数组就是你要的格式
------解决思路----------------------
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 />
Copy after login

指令串拆行看的清楚些,注意有大写的那些地方
------解决思路----------------------
<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 />
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