我有一个数组,它在两个日期之间每天输出以下内容。
所以基本上它会在2015年5月5日到2015年5月26日之间获得日期和结果,然后将在每周开始时安排它们。然后它将列出该周的所有7天,然后列出每天下的所有结果。
输出:http://pastebin.com/2SnKMgzG
正如您所看到的,这对于第一个嵌套周来说很好,但第二个嵌套周也包含第一周的结果。
&name=
我已经把它拆分了几周,每天这样,但是我不知道为什么第二周也包含第一周的数据呢?
有什么想法吗?
如果需要,我可以上传从$ loop数组返回的print_r()吗?
由于
答案 0 :(得分:0)
我假设您希望按周对帖子进行分组,并得到类似的内容:
$new_posts = array(
'2015-05-04' => array(
array('dt' => '2015-05-04', 'name' => 'first post', 'text' => 'blah, blah', ),
array('dt' => '2015-05-06', 'name' => 'second post', 'text' => 'blah, blah', ),
),
'2015-05-11' => array(
array('dt' => '2015-05-12', 'name' => 'third post', 'text' => 'blah, blah', ),
),
'2015-05-18' => array(
array('dt' => '2015-05-18', 'name' => 'fourth post', 'text' => 'blah, blah', ),
array('dt' => '2015-05-22', 'name' => 'fifth post', 'text' => 'blah, blah', ),
),
);
使用现有代码可以轻松实现这一目标:
$loop = getUsersHoursDetail("2015-05-04","2015-05-26","3");
$new_posts = array();
$merged = array();
foreach($loop as $post) {
$date = $post['dt'];
$week = date('W', strtotime($date));
$year = date('Y', strtotime($date));
$week_start = new DateTime();
$week_start->setISODate($year, $week);
$week_start = $week_start->format('Y-m-d');
// Group the posts by week in $new_posts
if (! isset($new_posts[$week_start]) ) {
// This is the first post of this week, initialize the week's list
$new_posts[$week_start] = array();
}
$new_posts[$week_start][] = $post;
// Group the posts by date in $merged
if (! isset($merged[$date])) {
// This is the first post of the day, initialize the day's list
$merged[$date] = array();
}
$merged[$date][] = $post;
}
此外,在$merged
中,您可以按类型数组中的日期对帖子进行分组。