循环到循环的变量传递,或者数据错误

Variable transfer for loop to loop, or data error

我现在来这里是为了一件事,它有一个名为 dayinfo 的数组。其中我要命名商品编号,可以在变量fixedinterval.date_id中找到商品编号。您知道以这种方式在禁止区传输数据的解决方案吗? 虽然问题可能出现在代码的其他地方

我的树枝代码:

   {% for fixintervall in days %}
        <tr>
            <th><p>
                {{ fixintervall.start_day  }}
                </p></th>

    {% for dayinfo in daysinfo %}
        {% for dayinfoti in dayinfo..fixintervall.date_id %}
            {% for dayin in dayinfoti %}
            <p>
                {{ dayin.start }} - {{ dayin.start2 }}
            </p>
            {% endfor %}
        {% endfor %}
    {% endfor %}

            <br>

        </tr>
        <br>
    {% else %}

控制器代码:

 #[Route('/cons/show/{id}', name: 'user_cons_show', methods: ['GET'])]
    public function show($id, ConsRepository $consRepository): Response
    {
        $showcon = array();
        $showconsintervall = array();
        $days = array();
        $dayid = array();
        $daysinfo = array();



        $con = $consRepository->find($id);
        $showconsintervall = $con->getIntervalls();

       foreach ($showconsintervall as $ex){
            $datumid = $ex->getId();
            $kezdodatum = $ex->getStart();
            $vegdatum = $ex->getEnd();
            $morepeople = $ex->getMore();
            $freetime = $ex->getFreeTime();
            $constime = $ex->getConsTime();
            $consulid = $id;

            /* Array in belüli array a cél az időpont listázás miatt */

            while($kezdodatum <= $vegdatum) {
                date_default_timezone_set($kezdodatum->getTimezone()->getName());
                if (date("Y-m-d",$kezdodatum->getTimestamp()) >= date('Y-m-d'))
                {
                    array_push(
                        $days, [
                        "start_day" => date("Y-m-d",$kezdodatum->getTimestamp()),
                        "date_id" => $datumid,
                    ]);

                    //logikai bukfenc

                    $start_a = date("H:i",$kezdodatum->getTimestamp());
                    $end_a = date("H:i",$vegdatum->getTimestamp());
                    $dat_a =  date("H:i",$constime->getTimestamp());
                    $up_a = date("H:i",$freetime->getTimestamp());


                    if (!array_key_exists($datumid, $daysinfo)){

                        $start = $start_a;
                        $end = $end_a;
                        $dat =  $dat_a;
                        $up = $up_a;
                        $start2 = $start;


                        $x = $datumid;
                        $x_data = array();

                       // echo "<br><br><br>Info:". $start2 ." - ". $end."<br>";
                        while ($start2 <= $end)
                        {


                            $start2_noft = (strtotime($start) + (strtotime($dat)) - strtotime('00:00:00'));
                            $start2 = date("H:i", $start2_noft);
                           // echo "Eleje:".$start." ".$start2." <br>";

                            if ($start2 <= $end){
                                array_push(
                                    $x_data,[
                                    "start" => $start,
                                    "start2" => $start2,
                                ]);
                            }

                            $start_noft = strtotime($start2) + strtotime($up) - strtotime('00:00:00');
                            $start = date("H:i", $start_noft);
                           // echo "Vége:".$start." ".$start2." <br>";
                        }

                        array_push(
                            $daysinfo,[
                            $x => $x_data
                        ]);
                    }
                }
                    $kezdodatum->modify('+1 day');
                }

            }


        array_push(
            $showcon, [
            "user_id" => $con->getUserId()->getId(),
            "cons_id" => $id,
        ]);

       print_r($daysinfo);

        return $this->render('user/cons_show.html.twig', [
            'controller_name' => 'Consulens',
            'showcon' => $showcon,
            'days' => $days,
            'dayid' => $dayid,
            'daysinfo' => $daysinfo,
        ]);
    }

而且问题毕竟是数据没有出现。至少间隔不是

结构 {% for x in y..z %} 用于循环 numbers/letters 的序列,如 documentation

中所示

这将 generate/compile 变成类似

的东西
<?php
    for($x = $y; $x < $z; $x++) {
        echo $x;
    }

由于您没有提供有关数组 dayinfoti 的太多信息,我假设 dayinfoti 的键将匹配 fixintervall.date_id。如果是这种情况,那么您可以像在 PHP

中那样使用此变量作为数组的键
{% for fixintervall in days %}
    ...
    ....
    {% for data in dayinfo[fixintervall.date_id]|default([]) %}
        ...
        ...
    {% endfor %}
{% endfor %}

default 过滤器将考虑 dayinfo
中缺失的元素,基本上,这消除了测试键 fixintervall.date_id 是否在数组 dayinfo 循环之前


More about dynamic keys/variables