1

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.

1 Answer 1

2

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'];

}

CodePad.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.