php - 使用 codeigniter 比较数组中的数据并计算它在数据库中出现的实例数

标签 php mysql arrays codeigniter activerecord

我有这个数组月 $monthsNum = ['1','2','3','4','5','6','7','8','9' ,'10','11','12'];,我希望在我的数据库列 month_uploaded 中将其与 1-12 之间的值进行比较。我希望它计算 $monthsNum 数组值出现在列 month_uploaded 中的实例数并将其存储在数组 $count_uploads 中。零计数是在数组 $count_uploads 中放置零值。我该怎么做?非常感谢您的帮助。谢谢你。以下是我的代码片段。

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);

}

输出:错误

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

期望的输出:

 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

这更接近,只需要为每个数组键获取正确的计数值

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);

}

输出:

 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

最佳答案

我不确定,但我认为您在这里需要的是 array_count_values。您只需使用 ->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);

已编辑:

根据结果数组,您可以将数组更新为

$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

关于php - 使用 codeigniter 比较数组中的数据并计算它在数据库中出现的实例数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31512107/

相关文章:

php - PHP 是否正式支持 "including"函数?

php - 我不小心删除了moodle(phpmyadmin)中的管理员用户,我无法访问网站管理中的任何内容

php - 在 PHP 中包含所有命名空间并仅使用类名访问类是否不好?

mysql - 在 'Where' 子句中执行两个子查询的良好替代方案

php - 拒绝直接访问 PHP 文件

MYSQL - 在 View 内多次调​​用函数时的性能问题

mysql - 快速查询数据库中过多的表

python - 如何对字符串数组执行 bincount?

php - in_array 函数未给出预期结果

c - 如何将 argv[][] 与空格字符进行比较?