Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare data in array and count the number of instances it occurs in the database using codeigniter

I have this array month $monthsNum = ['1','2','3','4','5','6','7','8','9','10','11','12'];, and I want this to be compared in my database column month_uploaded with values just ranging from 1-12. I want it to count the number of instances the $monthsNum array values occurs in column month_uploaded and stored it in array $count_uploads.Zero count is placed with zero value in array $count_uploads. How would I do that? Your help is much appreciated. Thank you. Below are pieces of my codes.

enter image description here

function count_uploads_perMonth(){

  $monthsNum = ['1','2','3','4','5','6','7','8','9','10','11','12'];

  $query = $this->db->select("*")
                    ->from($this->table_par)
                    ->where_in("month_uploaded",$monthsNum)
                    ->get();


      foreach( $query->result() as $row ){

        $count_uploads[] = count($row->month_uploaded);

      }

      var_dump($count_uploads);

}

Output: Wrong

array (size=5)
0 => int 1
1 => int 1
2 => int 1
3 => int 1
4 => int 1

Desired Output:

 array (size=12)
 0 => 0 or null
 1 => 0 or null
 2 => 0 or null
 3 => 0 or null
 4 => 0 or null
 5 => 1
 6 => 3 
 7 => 0 or null
 8 => 0 or null
 9 => 0 or null
10 => 0 or null
11 => 0 or null

This is closer, just have to get the right count value for each array key

function count_uploads_perMonth(){

  $monthsNum = array('1','2','3','4','5','6','7','8','9','10','11','12');
  $this->db->select("month_uploaded as cnt");
  $this->db->where_in('month_uploaded',$monthsNum);
  $this->db->group_by('month_uploaded');
  $query = $this->db->get($this->table_par);

  $count_uploads = array_fill(1, 12, 0);

    foreach( $query->result() as $row ){

      $count_uploads[$row->cnt] = $row->cnt;

    }
    var_dump($count_uploads);

}

Output:

 array (size=12)
 1 => int 0
 2 => int 0
 3 => int 0
 4 => int 0
 5 => int 0
 6 => string '6' (length=1)  --- value should be 1 and length is 1
 7 => string '7' (length=1)  --- value should be 4 and length is 4
 8 => int 0
 9 => int 0
 10 => int 0
 11 => int 0
 12 => int 0
like image 265
Eli Avatar asked Sep 28 '22 07:09

Eli


2 Answers

I'm not sure but I think what you need over here is array_count_values. You just make an array of result from your query using ->get()->result_array();

$array = range(1,12);
$result = array(7,7,7,6);
$final = array_count_values($result);
$final_array = array();
foreach($array as $value){
    $final_array[$value] = array_key_exists($value ,$final) ? $final[$value] : 0;
}
print_r($final_array);

Edited :

As per your resulted array you can update your array as

$arr = array(0 => array ('month_uploaded' =>  '7'),  1 => array ('month_uploaded' =>  '7'),  2 => array ('month_uploaded' =>  '7'), 3 => array ('month_uploaded' =>  '7'),  4 => array ('month_uploaded' =>  '6' ));
$result = array_count_values(array_column($arr,'month_uploaded'));
$final_array = array();
foreach($array as $value){
    $final_array[$value] = array_key_exists($value ,$result) ? $result[$value] : 0;
}
print_r($final_array);

Fiddle

like image 124
Narendrasingh Sisodia Avatar answered Oct 03 '22 02:10

Narendrasingh Sisodia


Append the count to array with months as keys.

$count_uploads[$row->month_uploaded][] = $row->month_uploaded;

Now, you will get array with month as key and appended values as values.

echo '<pre>';
print_r($count_uploads);
echo '</pre>';

Sample array

array(
  3 => array(3, 3),
  4 = array()
)
like image 32
Pupil Avatar answered Oct 03 '22 02:10

Pupil