来自具有codeigniter的数据库的关联数组

On my models I try to write a php model that will get me a associative array from a database. But I don't quite know how to approach this. So after I execute this SQL query:

SELECT balance_events.weight,balance_events.added_date,
       balance_entries.mid FROM balance_events, balance_entries 
WHERE balance_entries.added_date BETWEEN '2016-08-02' AND '2016-08-03' 
  AND balance_entries.ptid =12 
  AND balance_entries.beid = balance_events.id

I will get this table:

enter image description here

And from that table I want to extract a asociative array that it will look like this:

count = ['13'=>1, '6'=>4, '16'=>3, '4'=>3]

where 'mid'=>number of how many times that mid can be found in the table. ex. mid '13'=>1 cause you can found it only once.

I think that I will have to use SQL COUNT function, but how I can aggregate all of this in a PHP model in codeigniter? I know how to configure controller and view, but I don't know how to actually do the actual php model that will get me the desired array.

Try this query may help you ,

$result = $this->db->select('balance_events.weight,balance_events.added_date,COUNT(balance_entries.mid) as mid_count')
                   ->from('balance_events, balance_entries')
                   ->where('balance_entries.added_date BETWEEN "2016-08-02" AND "2016-08-03" ')
                   ->where('balance_entries.ptid','12')
                   ->where('balance_entries.beid','balance_events.id')
                   ->group_by('balance_entries.mid')
                   ->get();

return $result->result_array(); 

I'm not sure how you would create this in SQL but since you tagged php, I wrote a function that would do just this.

<?php
  $query = array(array("mid"=>13), array("mid"=>2), array("mid"=>13), array("mid" =>6), array("mid" => 13), array("mid" => 6));

  function createMidArray($queryResult){
      $returnArray = array();
      foreach ($queryResult as $qr){
          $returnArray[$qr['mid']]++;   
      }
  return $returnArray;
}

  print_r(createMidArray($query));

?>

The output of this was Array ( [13] => 3 [2] => 1 [6] => 2 ) which matches up to my inputted $query (which is a 2D array). I'm expecting the output of your query is stored in a similar array, but with more data and keys