Home  >  Article  >  Backend Development  >  数组 去重 统计问题 有点难度的计算方法

数组 去重 统计问题 有点难度的计算方法

WBOY
WBOYOriginal
2016-06-20 12:57:32791browse



如图 有一个数组 需要去重  重组数组的格式  在线讨论


回复讨论(解决方案)

贴出由 var_export 输出的示例数组,没有讨论的基础是讨论不起来的

其实很简单,你只要按聚类键聚类就可以了

贴出由 var_export 输出的示例数组,没有讨论的基础是讨论不起来的

其实很简单,你只要按聚类键聚类就可以了

这是现在的得到数组


希望 得到下面的格式


请问版主  这种怎么去 格式化

为什么要截图呢?难道要我一个个的去打字?

有什么要求?如果只要单纯实现的话,不可以用循环吗?

<meta charset="utf-8"><?php 	$arra = array(		0 => array(			'id' => 0,			'send_mobile'=> '13267310236',			'c_content'=>'天气很热注意防暑'		),		1 => array(			'id' => 1,			'send_mobile'=> '13267310285',			'c_content'=>'天气很热注意防暑'		),		2 => array(			'id' => 2,			'send_mobile'=> '13267310126',			'c_content'=>'天气很热注意防暑'		),		3 => array(			'id' => 3,			'send_mobile'=> '13267310166',			'c_content'=>'天气防暑'		)	);	$arrb = array();		foreach ($arra as $v){		$b = $v['c_content'];		if(isset($arrb[$b])) 			$arrb[$b]['send_mobile'] .= ','.$v['send_mobile'];		$arrb[$b] = isset($arrb[$b])?$arrb[$b]:$v;	}		$arrb = array_values($arrb);	print_r($arrb);?>


看你那个是用c_id做区分的吧,那就直接用cid做key, $arr[$cid][] = $val

提示你一下  中文可以作为数组键   

foreach ($arra as $k=>$v) { $res [$v[c_content]] = $v; } 
一句话ok

新建个数组b,遍历原数组a,如果遍历项中的c_content没有作为键名出现在b中,则插入一个b[c_content];否则把遍历到的send_mobile加进去,最后再把b数组整个重新键值排序一下。

<meta charset="utf-8"><?php 	$arra = array(		0 => array(			'id' => 0,			'send_mobile'=> '13267310236',			'c_content'=>'天气很热注意防暑'		),		1 => array(			'id' => 1,			'send_mobile'=> '13267310285',			'c_content'=>'天气很热注意防暑'		),		2 => array(			'id' => 2,			'send_mobile'=> '13267310126',			'c_content'=>'天气很热注意防暑'		),		3 => array(			'id' => 3,			'send_mobile'=> '13267310166',			'c_content'=>'天气防暑'		)	);	$arrb = array();		foreach ($arra as $v){		$b = $v['c_content'];		if(isset($arrb[$b])) 			$arrb[$b]['send_mobile'] .= ','.$v['send_mobile'];		$arrb[$b] = isset($arrb[$b])?$arrb[$b]:$v;	}		$arrb = array_values($arrb);	print_r($arrb);?>





谢谢  这个问题已经解决
Statement:
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