Count same values from wordpress database

My db looks like this:

timestamp                user_ip               user_agent
---------                -------               ----------
2015-09-15 01:11:11      111.111.111.111       bla bla Chrome bla bla
2015-09-15 02:22:22      222.222.222.222       bla bla Chrome bla bla
2015-09-15 04:44:44      222.222.222.222       bla bla Chrome bla bla
2015-09-15 05:55:55      222.222.222.222       bla bla Firefox bla bla

I need to count the same values of user_ip raw. Timestamp and user_agent are just for records inside db.
Expected result:

Read More
IP                 Count
---------------    -----
111.111.111.111      1
222.222.222.222      3

Thanks in advance.

Related posts

2 comments

  1. Try this:

     $arr = [
            0 => (object)[
              'timestamp' => '2015-09-15 04:43:40',
              'user_ip' => '104.131.92.125',
              'user_agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36 )'
              ],
            1 => (object)[
                'timestamp' => '2015-09-15 04:44:05',
                'user_ip' => '104.131.92.125',
                'user_agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36 )'
          ],
            2 => (object)[
                'timestamp' => '2015-09-15 04:44:05',
                'user_ip' => '102.131.92.125',
                'user_agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36 )'
            ]
    ];
        $a = json_encode($arr);
        $b = json_decode($a, true);
    
        $newArr = [];
        foreach($b as $key => $val)
        {
            if(array_key_exists('user_ip', $val)){
                $newArr[] = $val['user_ip'];
            }
    
        }
         echo '<pre>';
        $arrValues = array_count_values($newArr);
        print_r($arrValues);
    

    Here’s the result:

      Array
      (
        [104.131.92.125] => 2
        [102.131.92.125] => 1
      )
    

Comments are closed.