简体   繁体   中英

PHP Array with multiple values need a sort of Group By

i've already searched for a solution of my question, but I've not understood the answers at all, maybe because they're not useful for my question, but nevermind, I still haven't figured out how to do this.

I have an array structured like this:

Array ( 
    [Firefox] => 
     Array ( 
         [name] => Firefox 
         [clicks] => 0 
         [requests] => 5731 
           ) 
     ) 


Array ( 
        [Firefox] => 
         Array ( 
             [name] => Firefox 
             [clicks] => 0 
             [requests] => 5731 
               ) 
         ) 

Array ( 
            [Internet Explorer] => 
             Array ( 
                 [name] => Internet Explorer
                 [clicks] => 1 
                 [requests] => 1973
                   ) 
             ) 

generated by this part of code:

    $q1="SELECT r.ua, count(c.rid) as cnum, count(r.kwd) as rnum
    FROM requests r LEFT JOIN clicks_214 c ON r.id=c.rid
    WHERE hid='$hid' 
    AND r.time BETWEEN '$date1 $hour1:00:00' AND '$date2 $hour2:00:00'
    GROUP BY r.ua
    ORDER BY rnum DESC
    LIMIT $limit,50";
    $qr1=mysqli_query($conn,$q1) or die (mysqli_error());

    while(($r=mysqli_fetch_assoc($qr1))!=null)
    {
        $brow_obj=new Browser($r['ua']);
        $brow=$brow_obj ->getBrowser();
        $b_array[$brow]['name'] = $brow;
        $b_array[$brow]['clicks'] = $r['cnum'];
        $b_array[$brow]['requests'] = $r['rnum'];
    }

my problem is that all I have to do is a sort of GROUP BY browser name (Firefox, Internet Explorer, Chrome...) and sum all the clicks and the requests for that name obviously removing duplicates, for example:

Name: Firefox Clicks: all the clicks for Firefox Requests: all the requests for Firefox

Name: Internet Explorer Clicks: all the clicks for IE Requests: all the requests for IE

but isn't working at all here is the table structure:

request

id(PK),ua(user agent),kwd,hid(FK),time

click

id(PK),rid(FK)

Problem solved, it took some hours to think about this, but, indeed wasn't that difficult after all :)

foreach ($b_array as $item) 
{
    $key = $item['name'];
    if (!isset($browsers[$key])) 
    {
        $browsers[$key] = array(
            'name' => $item['name'],
            'clicks' => $item['clicks'],
            'requests' => $item['requests'],
        );
    } 
    else 
    {

        $browsers[$key]['clicks'] += $item['clicks'];
        $browsers[$key]['requests'] += $item['requests'];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM