我有一个数组,我需要按parentid
和time
按相反顺序进行分组。
Array
(
[0] => Array
(
[0] => 1
[ID] => 1
[1] => 87898
[totalsum] => 87898
[2] => 1234
[totalsuma] => 1234
[3] => 2017-01-07
[time] => 2017-01-07
[4] => parent1
[parentid] => parent1
[5] => user1
[username] => user1
)
[1] => Array
(
[0] => 2
[ID] => 2
[1] => 61405
[totalsum] => 61405
[2] => 0
[totalsuma] => 0
[3] => 2017-01-07
[time] => 2017-01-08
[4] => parent2
[parentid] => parent2
[5] => user2
[username] => user2
)
[2] => Array
(
[0] => 3
[ID] => 3
[1] => 53824
[totalsum] => 53824
[2] => 0
[totalsuma] => 0
[3] => 2017-01-08
[time] => 2017-01-08
[4] => parent1
[parentid] => parent1
[5] => user1
[username] => user1
)
[3] => Array
(
[0] => 4
[ID] => 4
[1] => 9313
[totalsum] => 9313
[2] => 0
[totalsuma] => 0
[3] => 2017-01-08
[time] => 2017-01-08
[4] => parent2
[parentid] => parent2
[5] => user3
[username] => user3
)
[4] => Array
(
[0] => 5
[ID] => 5
[1] => 6420
[totalsum] => 6420
[2] => 0
[totalsuma] => 0
[3] => 2017-01-08
[time] => 2017-01-08
[4] => parent1
[parentid] => parent1
[5] => user4
[username] => user4
)
[5] => Array
(
[0] => 6
[ID] => 6
[1] => 5826
[totalsum] => 5826
[2] => 0
[totalsuma] => 0
[3] => 2017-01-09
[time] => 2017-01-09
[4] => parent2
[parentid] => parent2
[5] => user2
[username] => user2 )
)
首先,我需要按相反顺序按time
进行分组,然后parentid
进行归待totalsum
和totalsuma
我需要在这样的表格中得到以下结果。
totalsum totalsuma parentid time
------------------------------------------------------------
5826 0 parent2 2017-01-09
60244 0 parent1 2017-01-08
70718 0 parent2 2017-01-08
87898 1234 parent1 2017-01-07
我可以尝试使用以下代码,但没有获得预期的结果,但我可以解决一些更改,但只有time
值,而不是time and parentid
if ( count($result) ) {
$groups = array();
$key = 0;
foreach ($result as $item) {
$key = $item['time'];
if (!array_key_exists($key, $groups)) {
$groups[$key] = array(
'totalsum' => $item['totalsum'],
'totalsuma' => $item['totalsuma'],
'parentid' => $item['parentid'],
'time' => $item['time'],
);
} else {
$groups[$key]['totalsum'] = $groups[$key]['totalsum'] + $item['totalsum'];
$groups[$key]['totalsuma'] = $groups[$key]['totalsuma'] + $item['totalsuma'];
$groups[$key]['time'] = $groups[$key]['time'] + $item['time'];
}
$key++;
}
echo "<pre>";
print_r($groups);
echo "</pre>";
} else {
echo "No rows returned.";
}
有人可以帮助实现这个目标吗?
提前致谢。