在以下情况下,我在按日期对数组元素进行分组时哪里出错了?
Where I'm making a mistake in datewise grouping array elements in following case?
我有一个名为 $all_event_details
的数组,如下所示:
Array
(
[0] => Array
(
[group_name] => Student Organisation
[event_id] => 239
[start_time] => 1443546000
[end_time] => 1443546000
)
[1] => Array
(
[group_name] =>
[event_id] => 237
[start_time] => 1443027600
[end_time] => 1443027600
)
[2] => Array
(
[group_name] => TestGroupShweta
[event_id] => 238
[start_time] => 1440784200
[end_time] => 1440784200
)
[3] => Array
(
[group_name] =>
[event_id] => 201
[start_time] => 1439596800
[end_time] => 1439596800
)
[4] => Array
(
[group_name] =>
[event_id] => 240
[start_time] => 1439596800
[end_time] => 1439596800
)
[5] => Array
(
[group_name] =>
[event_id] => 241
[start_time] => 1439596800
[end_time] => 1439596800
)
[6] => Array
(
[group_name] =>
[event_id] => 235
[start_time] => 1437130800
[end_time] => 1437134400
)
)
现在我想用相同的日期(即从键 'start_time' 中提取的日期)对所有数组元素进行分组。所以我做了以下代码来实现这一点。
$result = [];
foreach($all_event_details as $event) {
$event['event_group_date'] = date('d M, l Y',$event['start_time']);
$result[][] = $event;
}
print_r($result);
die;
但我得到以下数组作为输出,这是错误的(print_r($result);
的输出)。
Array
(
[0] => Array
(
[0] => Array
(
[group_name] => Student Organisation
[start_time] => 1443546000
[end_time] => 1443546000
[event_group_date] => 30 Sep, Wednesday 2015
)
)
[1] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 237
[start_time] => 1443027600
[end_time] => 1443027600
[event_group_date] => 24 Sep, Thursday 2015
)
)
[2] => Array
(
[0] => Array
(
[group_name] => TestGroupShweta
[event_id] => 238
[start_time] => 1440784200
[end_time] => 1440784200
[event_group_date] => 29 Aug, Saturday 2015
)
)
[3] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 201
[start_time] => 1439596800
[end_time] => 1439596800
[event_group_date] => 15 Aug, Saturday 2015
)
)
[4] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 240
[start_time] => 1439596800
[end_time] => 1439596800
[event_group_date] => 15 Aug, Saturday 2015
)
)
[5] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 241
[start_time] => 1439596800
[end_time] => 1439596800
[event_group_date] => 15 Aug, Saturday 2015
)
)
[6] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 235
[start_time] => 1437130800
[end_time] => 1437134400
[event_group_date] => 17 Jul, Friday 2015
)
)
)
但我想要遵循预期的数组输出。
Array
(
[0] => Array
(
[0] => Array
(
[group_name] => Student Organisation
[start_time] => 1443546000
[end_time] => 1443546000
[event_group_date] => 30 Sep, Wednesday 2015
)
)
[1] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 237
[start_time] => 1443027600
[end_time] => 1443027600
[event_group_date] => 24 Sep, Thursday 2015
)
)
[2] => Array
(
[0] => Array
(
[group_name] => TestGroupShweta
[event_id] => 238
[start_time] => 1440784200
[end_time] => 1440784200
[event_group_date] => 29 Aug, Saturday 2015
)
)
[3] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 201
[start_time] => 1439596800
[end_time] => 1439596800
[event_group_date] => 15 Aug, Saturday 2015
)
[1] => Array
(
[group_name] =>
[event_id] => 240
[start_time] => 1439596800
[end_time] => 1439596800
[event_group_date] => 15 Aug, Saturday 2015
)
[2] => Array
(
[group_name] =>
[event_id] => 241
[start_time] => 1439596800
[end_time] => 1439596800
[event_group_date] => 15 Aug, Saturday 2015
)
)
[4] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 235
[start_time] => 1437130800
[end_time] => 1437134400
[event_group_date] => 17 Jul, Friday 2015
)
)
)
谁能纠正我在代码中犯的错误,以便我可以获得预期的数组输出。
试试这个:(暂时无法验证)
<?php
$result = array();
foreach($all_event_details as $event) {
$event['event_group_date'] = date('d M, l Y',$event['start_time']);
$result[$event['event_group_date']][] = $event; //put $i in the index
}
$result = array_values($result);
print_r($result);
die;
?>
我有一个名为 $all_event_details
的数组,如下所示:
Array
(
[0] => Array
(
[group_name] => Student Organisation
[event_id] => 239
[start_time] => 1443546000
[end_time] => 1443546000
)
[1] => Array
(
[group_name] =>
[event_id] => 237
[start_time] => 1443027600
[end_time] => 1443027600
)
[2] => Array
(
[group_name] => TestGroupShweta
[event_id] => 238
[start_time] => 1440784200
[end_time] => 1440784200
)
[3] => Array
(
[group_name] =>
[event_id] => 201
[start_time] => 1439596800
[end_time] => 1439596800
)
[4] => Array
(
[group_name] =>
[event_id] => 240
[start_time] => 1439596800
[end_time] => 1439596800
)
[5] => Array
(
[group_name] =>
[event_id] => 241
[start_time] => 1439596800
[end_time] => 1439596800
)
[6] => Array
(
[group_name] =>
[event_id] => 235
[start_time] => 1437130800
[end_time] => 1437134400
)
)
现在我想用相同的日期(即从键 'start_time' 中提取的日期)对所有数组元素进行分组。所以我做了以下代码来实现这一点。
$result = [];
foreach($all_event_details as $event) {
$event['event_group_date'] = date('d M, l Y',$event['start_time']);
$result[][] = $event;
}
print_r($result);
die;
但我得到以下数组作为输出,这是错误的(print_r($result);
的输出)。
Array
(
[0] => Array
(
[0] => Array
(
[group_name] => Student Organisation
[start_time] => 1443546000
[end_time] => 1443546000
[event_group_date] => 30 Sep, Wednesday 2015
)
)
[1] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 237
[start_time] => 1443027600
[end_time] => 1443027600
[event_group_date] => 24 Sep, Thursday 2015
)
)
[2] => Array
(
[0] => Array
(
[group_name] => TestGroupShweta
[event_id] => 238
[start_time] => 1440784200
[end_time] => 1440784200
[event_group_date] => 29 Aug, Saturday 2015
)
)
[3] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 201
[start_time] => 1439596800
[end_time] => 1439596800
[event_group_date] => 15 Aug, Saturday 2015
)
)
[4] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 240
[start_time] => 1439596800
[end_time] => 1439596800
[event_group_date] => 15 Aug, Saturday 2015
)
)
[5] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 241
[start_time] => 1439596800
[end_time] => 1439596800
[event_group_date] => 15 Aug, Saturday 2015
)
)
[6] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 235
[start_time] => 1437130800
[end_time] => 1437134400
[event_group_date] => 17 Jul, Friday 2015
)
)
)
但我想要遵循预期的数组输出。
Array
(
[0] => Array
(
[0] => Array
(
[group_name] => Student Organisation
[start_time] => 1443546000
[end_time] => 1443546000
[event_group_date] => 30 Sep, Wednesday 2015
)
)
[1] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 237
[start_time] => 1443027600
[end_time] => 1443027600
[event_group_date] => 24 Sep, Thursday 2015
)
)
[2] => Array
(
[0] => Array
(
[group_name] => TestGroupShweta
[event_id] => 238
[start_time] => 1440784200
[end_time] => 1440784200
[event_group_date] => 29 Aug, Saturday 2015
)
)
[3] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 201
[start_time] => 1439596800
[end_time] => 1439596800
[event_group_date] => 15 Aug, Saturday 2015
)
[1] => Array
(
[group_name] =>
[event_id] => 240
[start_time] => 1439596800
[end_time] => 1439596800
[event_group_date] => 15 Aug, Saturday 2015
)
[2] => Array
(
[group_name] =>
[event_id] => 241
[start_time] => 1439596800
[end_time] => 1439596800
[event_group_date] => 15 Aug, Saturday 2015
)
)
[4] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 235
[start_time] => 1437130800
[end_time] => 1437134400
[event_group_date] => 17 Jul, Friday 2015
)
)
)
谁能纠正我在代码中犯的错误,以便我可以获得预期的数组输出。
试试这个:(暂时无法验证)
<?php
$result = array();
foreach($all_event_details as $event) {
$event['event_group_date'] = date('d M, l Y',$event['start_time']);
$result[$event['event_group_date']][] = $event; //put $i in the index
}
$result = array_values($result);
print_r($result);
die;
?>