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

Posted

技术标签:

【中文标题】比较数组中的数据并使用 codeigniter 计算它在数据库中出现的实例数【英文标题】:Compare data in array and count the number of instances it occurs in the database using codeigniter 【发布时间】:2015-10-09 07:35:54 【问题描述】:

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

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

【问题讨论】:

【参考方案1】:

将计数附加到以月份为键的数组中。

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

现在,您将获得以月份为键、附加值作为值的数组。

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

样本数组

array(
  3 => array(3, 3),
  4 = array()
)

【讨论】:

【参考方案2】:

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

【讨论】:

如何替换 $result = array(7,7,7,6); 中的值让它更有活力? 你能把$query的值贴出来吗 您显示的是单列结果而不是结果数组,只需将 -&gt;get() 替换为 -&gt;get()-&gt;result_array(); 并发布 print_r($query); 的值 array (size=5) 0 => array (size=1) 'month_uploaded' => string '7' (length=1) 1 => array (size=1) 'month_uploaded' = > 字符串 '7' (length=1) 2 => 数组 (size=1) 'month_uploaded' => 字符串 '7' (length=1) 3 => 数组 (size=1) 'month_uploaded' => 字符串 '7 ' (length=1) 4 => 数组 (size=1) 'month_uploaded' => 字符串 '6' (length=1) 如何将 $result = array(7,7,7,6) 中的值替换为数据库中的值以使其更具动态性?【参考方案3】:
Use following functions and get your result.

function count_uploads_perMonth()

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


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

    $count_uploads[] = $row->cnt;

  
  echo "<pre>";
  print_r($count_uploads);

 

【讨论】:

【参考方案4】:

将 SQL 查询更改为

SELECT month_uploaded-1 `month`, COUNT(*) `count` FROM table_name GROUP BY month_uploaded

您将收到 (5,1), (6,4)。那么

// Prepare array with zeros
$count_uploads = array_fill(0, 12, 0);

foreach( $query->result() as $row )
   $count_uploads[$row->month] = $row->count;

【讨论】:

以上是关于比较数组中的数据并使用 codeigniter 计算它在数据库中出现的实例数的主要内容,如果未能解决你的问题,请参考以下文章

在 CodeIgniter 3 中访问关联数组中的数据

如何使用codeigniter从mysql中获取多个多维数组中的数据?

如何使用 JQuery 将 HTML 表单字段作为数组直接传递到 CodeIgniter 中的数据库

将数组数据从视图传递到控制器并反向 php codeigniter

count():参数必须是在codeigniter中实现Countable的数组或者对象

如何将发布数据放入 CodeIgniter 中的数组中?帖子项目是数组