Carlos Barbosa Carlos Barbosa - 1 year ago 70
MySQL Question

Get array of unique values

hey there i have a page hits table that looks like:

browser || version || hits || referrer

i am already getting the hits array as in:

public function get_app_hits_array($app_fb_page_id, $app_type)
$app_analytics_hits_sql = "SELECT * FROM `analytics_hits` WHERE `fb_page_id` = '$app_fb_page_id' AND `app_type` = '$app_type'";
$result_app_analytics_hits_sql = mysql_query($app_analytics_hits_sql) or die("Query failed : " . mysql_error());
while ($app_analytics_hits_row = mysql_fetch_assoc($result_app_analytics_hits_sql)) {
$app_hits_array[] = $app_analytics_hits_row;

return $app_hits_array;

How can i generate a new array based on the unique records?, that stores, let's say in this case hits....

so i will need an array that will have:

browser || hits
chrome 30
safari 15
firefox 40

this should be generated preferentially from the first array that fetches all hits.

Answer Source

Without using aggregate MySQL functions, you could do...

$browserToHits = array();

foreach($results as $result) {
    $key = $result['browser'];

    if ( ! array_key_exists($key, $browserToHits)) {
        $browserToHits[$key] = 0;

    $browserToHits[$key] += $result['hits'];



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download