PHP foreach 仅在第一次迭代中显示值
PHP foreach showing values in only first iteration
我在 wordpress 中有一个自定义 post 类型的节目列表。我有一个我放在一起的自定义日历插件,但是在我的 for each 循环中从 post 中提取所有值时遇到问题。
当我启动 foreach 并打印出所有值时,它第一次显示一切正常,但之后的每次迭代都不会显示所有值(例如内容)。
for ($i = 1; $i <= $cal->days_in_month; $i++) {
echo '<li class="date_top"><span class="day">'.$i.'</span><ol>';
foreach ($cal->events as $event) {
if($i == date("j", $event['date_object']) &&
$cal->month_selected == date("n", $event['date_object'])) {
print_r($event);
}
}
echo '</ol></li>';
}
这是我从 post 中获取数据的函数。
function load_month_events() {
$query = $this->month_events();
foreach ($query as $post) {
$event = array();
$event['title'] = $post->post_title;
$event['date'] = get_post_meta($post->ID, 'start_date', true);
$event['end_date'] = get_post_meta($post->ID, 'end_date', true);
$event['permalink'] = get_permalink($post->ID);
$event['date_object'] = to_date_object($event['date']);
$content_post = get_post($post->ID);
$event['content'] = $content_post->post_content;
$shows_categories = wp_get_post_terms($post->ID, 'shows_category');
$showseason_string = "";
foreach($shows_categories as $category) {
$showseason_string = $showseason_string . $category->slug . " ";
}
$event['showseason'] = $showseason_string;
$this->events[] = $event;
}
$this->duplicate_multiday();
usort($this->events, "cmp");
}
以及一个输出示例(它正确调用了一个月中的所有 31 天并列出了当天发生的事件,但没有提取内容和结束日期。)
1
Array ( [title] => A choirs line [date] => 20150101 [end_date] => 20150131 [permalink] => http://localhost:8888/shows/a-choirs-line/ [date_object] => 1420070400 [content] => A Choirs Line Content Goes Here [showseason] => 2014-2015-season )
2
Array ( [title] => A choirs line [permalink] => http://localhost:8888/shows/a-choirs-line/ [showseason] => 2014-2015-season [date] => 20150102 [date_object] => 1420156800 )
提前感谢您提供的任何指导!我觉得我以前遇到过这个,但我不记得我是如何解决它的,此时我正在用头撞墙。
编辑: 修复是将数组字段添加到函数 duplicate_multiday
function duplicate_multiday() {
foreach($this->events as $event) {
if($event['end_date'] && $event['date'] != $event['end_date']) {
$date1 = new DateTime($event['date']);
$date2 = new DateTime($event['end_date']);
$interval = $date1->diff($date2);
for($x=1; $x <= $interval->days; $x++) {
$new_event = array();
$new_event['title'] = $event['title'];
$new_event['permalink'] = $event['permalink'];
$new_event['content'] = $event['content'];
$new_event['end_date'] = $event['end_date'];
$new_event['showseason'] = $event['showseason'];
error_log($event['showseason']);
$new_event['date'] = date_format($date1->add(new DateInterval('P1D')), "Ymd");
$new_event['date_object'] = to_date_object($new_event['date']);
$this->events[] = $new_event;
}
}
}
}
你在self::duplicate_multiday()
中修改了$this->events
的数据,你在其中复制了事件,但是你没有复制一些信息,比如你正在复制的“content
”数组字段失踪。
我在 wordpress 中有一个自定义 post 类型的节目列表。我有一个我放在一起的自定义日历插件,但是在我的 for each 循环中从 post 中提取所有值时遇到问题。
当我启动 foreach 并打印出所有值时,它第一次显示一切正常,但之后的每次迭代都不会显示所有值(例如内容)。
for ($i = 1; $i <= $cal->days_in_month; $i++) {
echo '<li class="date_top"><span class="day">'.$i.'</span><ol>';
foreach ($cal->events as $event) {
if($i == date("j", $event['date_object']) &&
$cal->month_selected == date("n", $event['date_object'])) {
print_r($event);
}
}
echo '</ol></li>';
}
这是我从 post 中获取数据的函数。
function load_month_events() {
$query = $this->month_events();
foreach ($query as $post) {
$event = array();
$event['title'] = $post->post_title;
$event['date'] = get_post_meta($post->ID, 'start_date', true);
$event['end_date'] = get_post_meta($post->ID, 'end_date', true);
$event['permalink'] = get_permalink($post->ID);
$event['date_object'] = to_date_object($event['date']);
$content_post = get_post($post->ID);
$event['content'] = $content_post->post_content;
$shows_categories = wp_get_post_terms($post->ID, 'shows_category');
$showseason_string = "";
foreach($shows_categories as $category) {
$showseason_string = $showseason_string . $category->slug . " ";
}
$event['showseason'] = $showseason_string;
$this->events[] = $event;
}
$this->duplicate_multiday();
usort($this->events, "cmp");
}
以及一个输出示例(它正确调用了一个月中的所有 31 天并列出了当天发生的事件,但没有提取内容和结束日期。)
1
Array ( [title] => A choirs line [date] => 20150101 [end_date] => 20150131 [permalink] => http://localhost:8888/shows/a-choirs-line/ [date_object] => 1420070400 [content] => A Choirs Line Content Goes Here [showseason] => 2014-2015-season )
2
Array ( [title] => A choirs line [permalink] => http://localhost:8888/shows/a-choirs-line/ [showseason] => 2014-2015-season [date] => 20150102 [date_object] => 1420156800 )
提前感谢您提供的任何指导!我觉得我以前遇到过这个,但我不记得我是如何解决它的,此时我正在用头撞墙。
编辑: 修复是将数组字段添加到函数 duplicate_multiday
function duplicate_multiday() {
foreach($this->events as $event) {
if($event['end_date'] && $event['date'] != $event['end_date']) {
$date1 = new DateTime($event['date']);
$date2 = new DateTime($event['end_date']);
$interval = $date1->diff($date2);
for($x=1; $x <= $interval->days; $x++) {
$new_event = array();
$new_event['title'] = $event['title'];
$new_event['permalink'] = $event['permalink'];
$new_event['content'] = $event['content'];
$new_event['end_date'] = $event['end_date'];
$new_event['showseason'] = $event['showseason'];
error_log($event['showseason']);
$new_event['date'] = date_format($date1->add(new DateInterval('P1D')), "Ymd");
$new_event['date_object'] = to_date_object($new_event['date']);
$this->events[] = $new_event;
}
}
}
}
你在self::duplicate_multiday()
中修改了$this->events
的数据,你在其中复制了事件,但是你没有复制一些信息,比如你正在复制的“content
”数组字段失踪。