简体   繁体   English

如果$ i == 1,则在foreach循环中的php回显“ ok”,否则回显“ ok”一次并继续

[英]Php inside foreach loop if $i == 1 echo “ok”, else echo “ok” JUST one time and continue

its been a while (5 years) since my last time here making questions. 自从我上次在这里提问以来已经有一段时间了(5年)。

I need help, I'm developing an event calendar and I'm stuck in this. 我需要帮助,我正在开发活动日历,因此陷入困境。 I want to have event header and event body, so far so good, but I just want one header if there are more than one event in each day. 到目前为止,我想拥有事件标头和事件主体,但是如果每天有多个事件,我只想要一个标头。 So today I've a dinner at 21h, my event should have Friday , November 23 and Dinner at 21h. 因此,今天我在21点共进晚餐,我的活动应该在11月23日星期五和21点共进晚餐。 Perfect, I can do this, but what about if I do have a meeting before dinner, at 19h? 太好了,我可以这样做,但是如果我在晚餐前19点开会,该怎么办? I want just one time the same header ... Friday, November 23 but 2 bodies... 我只想要一次相同的标头... 11月23日,星期五,但有两个尸体...

thanks! 谢谢!

my piece of code 我的一段代码

foreach($eventos[$data_do_dia] as $evento) {
    $i = count($eventos[$data_do_dia]);
    if($i == 1) {
       $agenda .= " dia ".date("d-m-Y",strtotime($evento['dia']))."</b>";
       add the event
    } else {
        repeat the same but just one time
        $agenda .= " dia ".date("d-m-Y",strtotime($evento['dia']))."</b>";

        add event one
        add event two
        ...     
    }
}

I've changed the loop so that there isn't any repeated code, this basically means that if there is only one event, it converts into an array, assuming that isset($evento[0]) will pick up there aren't sub-arrays, it wraps the one entry into a new array. 我已经更改了循环,因此没有重复的代码,这基本上意味着,如果只有一个事件,则它将转换为数组,并假设isset($evento[0])将不会出现。子数组,它将一个条目包装到新数组中。

It then loops over the array and creates the string with the date/time/details. 然后,它遍历数组并创建带有日期/时间/详细信息的字符串。 You will probably want to change the format, but the principle is the same... 您可能会想要更改格式,但是原理是相同的...

$agenda = "";
foreach($eventos as $evento) {
    if ( !isset($evento[0]) )    {
        $evento = [$evento];
    }
    $agenda .= " dia ".date("d-m-Y",strtotime($evento[0]['dia'])).PHP_EOL;
    foreach ( $evento as $time )    {
        $agenda .= " time ".$time['hora']." ".
            $time['titulo']." ".$time['evento'].PHP_EOL;
    }
}
echo $agenda;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM