I'm using datetime to store dates of when new data is entered per provider. Using the following query to pull the results:
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
The array looks like this:
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"
I need the results to count the total of "addedCount" where "dateAdded" and "provider" match so that the final array looks like:
addedCount dateAdded provider
3 2012-03-03 provider_1
2 2012-03-03 provider_2
I've tried tirelessly searching SO and rebuilding the arrays, but after many hours spent, I ended up rebuilding the array in its original form,
Thank you in advance!
You can use the data as indexe in order to group similar data for example:
$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/>" );
}