I'm getting the desired results back from my query and now I'm having trouble figuring out how to manipulate the data into something usable.
The ultimate goal is a json output to be used by google charts and I can do this on any one single component without a problem.
I need the output to look like this (I have no idea how many components will come after Month/Year... it might be 0 or 10 or 20...):
['Month/Year', 'Wiper Blades', 'Mufflers'] [,[...]],
['March 2013', 13, 11],
['April 2013', 17, 19],
[...],
[...]
]);
Here is the array I have after my query ( can't do this in a while() because I need the cmpnt_name for the first line of the desired output (above) which is why I'm building the array first):
Array
(
[0] => Array
(
[vmonth] => February
[vyear] => 2013
[cmpnt_count] => 9
[cmpnt_name] => Wiper blade
)
[1] => Array
(
[vmonth] => March
[vyear] => 2013
[cmpnt_count] => 13
[cmpnt_name] => Wiper blade
)
[2] => Array
(
[vmonth] => March
[vyear] => 2013
[cmpnt_count] => 11
[cmpnt_name] => Muffler
)
[3] => Array
(
[vmonth] => April
[vyear] => 2013
[cmpnt_count] => 17
[cmpnt_name] => Wiper blade
)
[4] => Array
(
[vmonth] => April
[vyear] => 2013
[cmpnt_count] => 19
[cmpnt_name] => Muffler
)
)
I can't loop through the array and take every cmpnt_name and append that to the first line of the desired output... I only want uniques.
Also a component may not have any views for a given month (it may not have even existed that month) so I may have components that date from Feb and other components that don't get added until later but are included in the count... i.e. Muffler was added in March.
So how do I go about finding the unique component names for the first row?
I was thinking:
function findUniques($cmpnt)
{
$names = array();
foreach($cmpnt as $item)
{
$key = $item['cmpnt_name'];
if(array_key_exists($key, $names))
{
$names[$key]++;
}
else
$names[$key] = 1;
}
return($names);
}
Is there a php function for this?
Is there a better way to do this?