PHP按日期对数据进行分组并将它们放入数组中

时间:2018-09-04 23:01:50

标签: php codeigniter

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

输出:

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

这是我的尝试:

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

但是结果是这样的:

Array
(
    [2018-08] => Array
        (
            [penalty] => 0
            [warning] => 3
        )

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

)

问题是他八月只有3次警告,9月只有2次警告和1次罚款,这将是我想要的输出,我将如何使它变成这样?

1 个答案:

答案 0 :(得分:0)

您在这里(未经测试):

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

正则表达式

  • -从字面上匹配-
  • \d匹配任意数字
  • {2}正好2次
  • $匹配字符串的结尾。

简而言之,它代替了2018-08 -31部分。如果您确定日期将始终是有效日期,请使用日期功能。如果不是,或者您不希望使用日期,则可以使用正则表达式。

作为旁注,如果您使用var_export而不是print_r来输出数据,我会进行测试,因为我懒于格式化数据。这些天之一,我会写点东西给我转换。

作为第二点,我添加了标题(在注释中),如果ajax回调未正确解析您的数据,则可以使用它,它应该作为对象出现,就像应该的那样。但是,某些较旧的浏览器可能与此有关,例如IE8或IE9等。由于他们不了解,他们可能会尝试下载JSON文件。因此,这取决于您是否打算支持这些。

干杯。