我正在使用datetime来存储每个提供商输入新数据的日期。使用以下查询来提取结果:
SELECT COUNT(id) AS addedCount,
date(date_added) AS dateAdded,
provider_name as provider
FROM `table` WHERE `table`.`id` IN (SELECT `table`.`id` FROM `table`
WHERE `table`.`active` = '1')
GROUP BY dateAdded
ORDER BY dateAdded DESC
数组如下所示:
array(1343) {
[0]=>
array(3) {
["addedCount"]=>
string(1) "2"
["dateAdded"]=>
string(10) "2012-03-03"
["provider"]=>
string(5) "Provider_1"
}
[1]=>
array(3) {
["addedCount"]=>
string(1) "1"
["dateAdded"]=>
string(10) "2012-03-03"
["provider"]=>
string(5) "Provider_1"
}
[2]=>
array(3) {
["addedCount"]=>
string(1) "1"
["dateAdded"]=>
string(10) "2012-03-03"
["provider"]=>
string(5) "Provider_2"
}
[3]=>
array(3) {
["addedCount"]=>
string(1) "1"
["dateAdded"]=>
string(10) "2012-03-03"
["provider"]=>
string(5) "Provider_2"
我需要结果来计算“addedCount”的总数,其中“dateAdded”和“provider”匹配,以便最终的数组看起来像:
addedCount dateAdded provider
3 2012-03-03 provider_1
2 2012-03-03 provider_2
我已经尝试不知疲倦地搜索SO并重建数组,但是花了好几个小时后,我最终重建了原始形式的数组,
提前谢谢!
答案 0 :(得分:2)
您可以将数据用作索引,以便对类似数据进行分组,例如:
$groupArray = array();
foreach ( $providerArr as $provider )
{
// Using the grouping info (date & name) as a key to our grouping array
$key = $provider[ 'dateAdded' ] . $provider[ 'provider' ];
if ( !isset( $groupArray[ $key ] ) )
{
// Our grouping array does not have the specific combination of date & name yet
// so save the first occurance of data for this combination.
$groupArray[ $key ] = $provider;
}
else
{
// The unique combination of date & name already exists so just update the count
$groupArray[ $key ][ 'addedCount' ] += $provider[ 'addedCount' ];
}
}
// Now print
foreach ( $groupArray as $data )
{
echo( "{$data[ 'addedCount' ]} - {$data[ 'dateAdded'} - {$data[ '$provider' ]}<br/>" );
}