我来自 PHP 世界,我需要做一些记录统计。在 php 中我知道这很简单,但是如何在 Groovy 中执行以下操作
$logs = array( array( 'date' => '5-15', 'name' => 'foo' ...other stuff),
array( 'date' => '5-15', 'name' => 'bar' ...other stuff),
array( 'date' => '5-16', 'name' => 'foo' ...other stuff),
array( 'date' => '5-17', 'name' => 'foo' ...other stuff),
array( 'date' => '5-17', 'name' => 'foo' ...other stuff),
array( 'date' => '5-17', 'name' => 'bar' ...other stuff),
array( 'date' => '5-17', 'name' => 'bar' ...other stuff) );
$counts = array();
foreach($logs as $log) {
if( isset($counts[ $log['date'] ][ $log['name'] ]) ) {
$counts[ $log['date'] ][ $log['name'] ] = 1;
} else {
$counts[ $log['date'] ][ $log['name'] ]++;
}
}
这给了我结果
['5-15']['foo'] = 1
['5-15']['bar'] = 1
['5-16']['foo'] = 1
['5-17']['foo'] = 2
['5-17']['bar'] = 2
日志实际上是我从 GORM 查询返回的结果集。