0

我想回顾一下我原来的问题:使用 PHP foreach 代码填充日历

在本地主机服务器上工作时,脚本可以工作,但是在线上传时,日历没有出现,Chrome 给我以下错误:

<b>Warning</b>:  array_values() [<a href='function.array-values'>function.array-values</a>]: The argument should be an array in <b>/home/flyeurov/public_html/lib/skins/flyeuro/events/events_index.tpl</b> on line <b>30</b>
<b>Warning</b>:  array_merge() [<a href='function.array-merge'>function.array-merge</a>]: Argument #2 is not an array in <b>/home/flyeurov/public_html/lib/skins/flyeuro/events/events_index.tpl</b> on line <b>30</b>
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/home/flyeurov/public_html/lib/skins/flyeuro/events/events_index.tpl</b> on line <b>30</b><br />

我发现当我过去没有发生任何事件 =$history因此是空的,或者为未来计划的事件= 空时,就会发生这种情况$events

foreach(array_merge(array_values($history), array_values($events)) as $event)

但是我的系统有时注定没有计划任何事件,因此是空$event的,所以我的问题是,我怎样才能绕过 foreach 以使用一个或另一个空变量来显示日历?

4

2 回答 2

2
if(!empty($history) || !empty($events)){
   foreach(array_merge(array_values($history), array_values($events)) as $event){...}
}

在您的情况下,这将是一个更好的解决方案(删除了 if 条件):

$history = (!empty($history))?$history:array();
$events = (!empty($events))?$events:array();
foreach(array_merge(array_values($history), array_values($events)) as $event){...}
于 2013-02-21T12:35:28.920 回答
0

if (!empty($array)) { ... }

这将在 foreach 之前进行检查。

于 2013-02-21T12:35:56.567 回答