首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP将数据按日期分组,并将其放入数组中

PHP将数据按日期分组,并将其放入数组中
EN

Stack Overflow用户
提问于 2018-09-05 07:01:50
回答 1查看 168关注 0票数 0

我的数据库里有这个数据,这是给这个学生的

输出:

代码语言:javascript
复制
Array
(
    [0] => stdClass Object
        (
            [id] => 18
            [student_id] => 97
            [date] => 2018-08-31
            [type] => Intersexual Dormitory Visit
            [amount] => 0
            [count] => 3
            [remark] => 
            [reg_date] => 2018-09-04 16:50:16
        )

    [1] => stdClass Object
        (
            [id] => 17
            [student_id] => 97
            [date] => 2018-09-04
            [type] => Bringing or Drinking alcohol
            [amount] => 0
            [count] => 1
            [remark] => 
            [reg_date] => 2018-09-04 16:48:54
        )

    [2] => stdClass Object
        (
            [id] => 16
            [student_id] => 97
            [date] => 2018-09-04
            [type] => Throwing away wastes
            [amount] => 0
            [count] => 1
            [remark] => 
            [reg_date] => 2018-09-04 15:52:09
        )

    [3] => stdClass Object
        (
            [id] => 15
            [student_id] => 97
            [date] => 2018-09-04
            [type] => Class Late
            [amount] => 300
            [count] => 1
            [remark] => absent 1 class
            [reg_date] => 2018-09-04 15:46:19
        )

)

我想做的是根据date (Year and Month)对它们进行分组,当amount为0时,它被计为warning,当大于0时,它被计为penalty

这是我的尝试:

代码语言:javascript
复制
public function getChart($in_data)
{
    $this->load->model('student_model');

    $student_info = $this->student_model->selectItem($in_data);

    $count_warning = 0;
    $count_penalty = 0;

    $chart = array();
    $items = parent::getItems(array('student_id'=>$student_info->member_id)); //this is for getting the Output

    foreach ($items as $item) {

        $count = array('penalty'=>0, 'warning'=>0);

        if ($item->amount > 0) {
            $count['penalty'] += $item->count;
        } else {
            $count['warning'] += $item->count;
        }

        $chart[date('Y-m', strtotime($item->date))] = $count;
    }

    return $chart;
}

但结果是这样的:

代码语言:javascript
复制
Array
(
    [2018-08] => Array
        (
            [penalty] => 0
            [warning] => 3
        )

    [2018-09] => Array
        (
            [penalty] => 1
            [warning] => 0
        )

)

问题是他在8月份只有3次警告,9月份有2次警告和1次处罚,这将是我想要的输出,我怎么才能做到这样呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-05 07:20:38

这就是(未测试的):

代码语言:javascript
复制
foreach ($items as $item) {
      //just because I can (use array key for ease of accessing it)
      $key = preg_replace('/-\d{2}$/', '', $item['date']);
      //you can use this instead if you want.
      //$key = date('Y-m', strtotime($item->date));

       //we can join our ifs tougher as the deal with the same elements
       //and this will make the code more efficient
      if(!isset($chart[$key])){
         //add $key as month
         $chart[$key] = array('penalty'=>0, 'warning'=>0, 'month'=>$key);         
      }else if ($item->amount > 0) {
         $chart[$key]['penalty'] += $item->count;
      } else {
         $chart[$key]['warning'] += $item->count;
      }

}

 //may want to put the proper content type
 //this tells the ajax call what it is and will parse it 
 //automatically in most browsers, so you can toss that JSON.parse().
 //CAUTION:do not output data before a header call
 //header('Content-type: application/json'); 

 //array_values removes the date key, its easier to built array with it.
 //then we just output the json
 echo json_encode(array_values($chart));

Regex Test

正则表达式

  • - match literally
  • \d match any digit
  • {2} 2 times
  • $ match - end of string。

简而言之,它取代了2018-08年的-31部分。如果您确定日期始终是有效日期,请使用日期函数。您可以使用正则表达式,如果它不是或者您不想使用日期...

顺便说一句,如果您使用var_export而不是print_r来输出数据,我会对其进行测试,因为我懒得格式化数据。总有一天我会写些东西来帮我转换它。

作为第二个附注,我添加了头(在注释中),如果ajax回调不能正确解析您的数据,那么您可以使用它,它应该作为一个对象出来,就像它应该做的那样。然而,一些老的浏览器可能在这方面有问题,比如IE8或者IE9等等。因为他们不理解它,所以他们可能会尝试下载一个JSON文件。因此,这取决于您是否计划支持这些。

干杯。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52175024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档