php数组的排重、累加、排序处理,50分全部送上!
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> $t = array ( array( 'cid' => 123, 'hits' => 200, ), array( 'cid' => 456, 'hits' => 100, ), array( 'cid' => 789, 'hits' => 300, ), array( 'cid' => 123, 'hits' => 600, ), array( 'cid' => 456, 'hits' => 500, ), array( 'cid' => 789, 'hits' => 700, ), );
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> $t = array ( array( 'cid' => 789, 'hits' => 1000, ), array( 'cid' => 123, 'hits' => 800, ), array( 'cid' => 456, 'hits' => 600, ), );
foreach($t as $v){
if(!$ar[$v['cid']])
$ar[$v['cid']]=$v;
else
$ar[$v['cid']]['hits']+=$v['hits'];
}
foreach($ar as $v) $k[]=$v[hits];
array_multisort($k,SORT_DESC,$ar);
print_r($ar);
<br><font color="#e78608">------解决方案--------------------</font><br>
$t = array (
array(
'cid' => 123,
'hits' => 200,
),
array(
'cid' => 456,
'hits' => 100,
),
array(
'cid' => 789,
'hits' => 300,
),
array(
'cid' => 123,
'hits' => 600,
),
array(
'cid' => 456,
'hits' => 500,
),
array(
'cid' => 789,
'hits' => 700,
),
);
$r = array();
foreach($t as $v) {
if(!isset($r[$v['cid']])) $r[$v['cid']] = $v;
else $r[$v['cid']]['hits'] += $v['hits'];
}
usort($r, create_function('$a,$b', 'return $a["hits"]
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号