PHP根据数据的日期对数据进行分组,并将其放入数组中
我的数据库里有这些数据,这是给这个学生的 输出:PHP根据数据的日期对数据进行分组,并将其放入数组中,php,codeigniter,Php,Codeigniter,我的数据库里有这些数据,这是给这个学生的 输出: Array ( [0] => stdClass Object ( [id] => 18 [student_id] => 97 [date] => 2018-08-31 [type] => Intersexual Dormitory Visit [amount] => 0
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
)
)
我想做的是根据日期(年和月)
对它们进行分组,当金额
为0时,将其计为警告
,当大于0时,将其计为罚款
这是我的尝试:
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
)
)
问题是他在8月份只有3次警告,9月份有2次警告和1次处罚,这将是我想要的结果,我将如何做到这一点?给你(未经测试):
正则表达式
按字面意思匹配-
-
匹配任意数字\d
正好两次{2}
匹配字符串的结尾$
var\u export
而不是print\u r
来输出数据,我会测试它,因为我懒得格式化它。总有一天我会写一些东西来为我转换它
作为第二个补充说明,我添加了标题(在注释中),如果您的数据没有被ajax回调正确解析,那么您可以使用它,它应该作为一个对象出现,就像它应该的那样。然而,一些较老的浏览器可能会遇到这个问题,比如IE8或者IE9等等。因为他们不理解它,他们可能会尝试下载一个JSON文件。所以这取决于你是否计划支持这些
干杯。我在8月份看到3条警告。为什么你会把一个月内的3算作1?是的,我的错,我编辑了它,很好,但我想用json格式,我希望输出像这样,
{“月”:“2018-08”,“惩罚”:0,“警告”:3},{“月”:“2018-09”,“惩罚”:1,“警告”:2}
告诉我在问题中它说了什么。我毕竟不是通灵者。此外,你还可以做json_encode($chart)
,很简单。对不起,我用这个$chart[$key]=array('month'=>date('Y-m',strotime($item->date)),'pould'=>0,'warning'=>0)定制了你的代码代码>和json_encode($chart)
的结果是:{“2018-08”:{“月”:“2018-08”,“惩罚”:0,“警告”:3},“2018-09”:{“月”:“2018-09”,“惩罚”:1,“警告”:2}
但只希望输出与第一条注释相同。对不起,先生,我修好了。使用array\u values
删除顶级键,然后进行编码,并将该键作为月份输入。当我读到这个问题时,我认为您希望在底部使用数组,您也应该发布JSON。哦,没什么大不了的。
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));