简体   繁体   English

Foreach在foreach里面

[英]Foreach inside of a foreach

I need help with a foreach within a foreach. 我需要在foreach中帮助foreach。 I'm using the Wordpress plug-in, Advanced Custom Fields to create Repeater Fields inside of repeater fields. 我正在使用Wordpress插件,高级自定义字段在转发器字段内创建转发器字段。 I'm then displaying (or attempting to display) them on the frontend outside the main loop using the code below: 然后我使用下面的代码在主循环外的前端显示(或试图显示)它们:

 <?php
            //Loop Through Days and Sights
              $days = get_field('day_and_highlights');
              $n = 0;

              if($days){
                echo '<div>';
                  foreach($days as $day){ 
                    $n++;
                    echo '<h2> Day ' . $n . ': ' . $day['destination_dropdown'] . '</h2><br/>';
                    //Loop through Attractions
                    <?php
                    $attractions = get_field('attraction_list');
                    if($attractions){
                        echo '<ul class=\"list_attractions\">';
                        foreach($attractions as $attraction){
                            echo '<li class=\"single_attraction\"><p>' . $attraction['add_attraction'] . '</p></li>';
                        }; //end foreach
                        echo '</ul>';
                    }; //endif 
                  };
                echo '</div>';
              };
            ?>

The first foreach echo's everything spot on. 第一个foreach echo的一切都在发现。 The inner foreach doesn't do jack. 内在的foreach不做千斤顶。 I can't get it to do anything. 我无法做任何事情。

You have an extra php tag: 你有一个额外的PHP标签:

             //Loop through Attractions
             <?php
                $attractions = get_field('attraction_list');
                if($attractions){

Try removing it. 尝试删除它。

Also, your IF and FOREACH statements should not end in semicolons: 此外,您的IF和FOREACH语句不应以分号结尾:

                }; //endif 

http://www.php.net/manual/en/control-structures.foreach.php http://www.php.net/manual/en/control-structures.foreach.php

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

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