doutuanxiao4619 2015-05-14 16:18
浏览 83
已采纳

有效地为多个PHP foreach循环使用相同的代码

I have a few different sets of data that go through a foreach loop. A lot of the code inside each foreach loop is similar, almost identical, but I'm using variables based on previously defined arrays as well as on that initial data put into the foreach loop. Is there a way to combine these loops into some sort of function without having to pass all of that data into the function?

Meaning, can I define a function within a loop and have it use that data in some way? I need to do this because if I want to display only those rows that have associated data, I loop through using the data. But if I want to display all rows whether there is associated data for it or not, then I need to use a different array to loop through.

I need to do two separate loops for the else because it needs to get layout items common to all arrangements and layout items specific to this arrangement, and then if there's data I need to display it one way, and if it's empty, I need to do something different, so I have to check for that as well.

Here's an example of the multiple loops using the same data:

if($hideempty == 'hide'){
    foreach($bannerArray as $bannerID => $records){
         foreach($records as $id => $record){ ?>
              <tr>
                  <td><?php echo $record['layout_name']; ?></td>
                  <td><?php echo $record['company_name']; ?></td>
               </tr>
          <?php }
     }
 } else {
      $layoutGroupArray = mysqli_fetch_assoc(mysqli_query($link, "SELECT `set_layout_group` FROM `SetTopics` WHERE `set_id` = $setID"));
      $layoutGroup = $layoutGroupArray['set_layout_group'];
      foreach($banners[0] as $bannerID => $layoutInfo){
          if(isset($bannerArray[$bannerID])){
              foreach($bannerArray[$bannerID] as $id => $record){ ?>
                  <tr>
                      <td><?php echo $record['layout_name']; ?></td>
                      <td><?php echo $record['company_name']; ?></td>
                  </tr>
               <?php }
           } else { ?>
               <tr>
                   <td><?php echo $layoutInfo['layout_name']; ?></td>
                   <td>empty</td>
               </tr>
           <?php }
       }
       foreach($banners[$layoutGroup] as $bannerID => $layoutInfo){
           if(isset($bannerArray[$bannerID])){
               foreach($bannerArray[$bannerID] as $id => $record){ ?>
                   <tr>
                       <td><?php echo $record['layout_name']; ?></td>
                       <td><?php echo $record['company_name']; ?></td>
                   </tr>
               <?php }
           } else { ?>
               <tr>
                   <td><?php echo $layoutInfo['layout_name']; ?></td>
                   <td>empty</td>
               </tr>
           <?php }
       }
  }

It doesn't have to be a function that I use, but I'm not sure how to write this code efficiently. I am doing a lot of repeating, but I can't come up with another way to access the data I need without doing this repeating.

  • 写回答

4条回答 默认 最新

  • douguiyan9164 2015-05-14 16:27
    关注

    Why not using a function?

    function layout_company($layout, $company) { ?>
    <tr>
        <td><?=$layout?></td>
        <td><?=$company?></td>
    </tr>
    <?php }
    
    function show_records(array $records) {
        foreach($records as $id => $record) {
            layout_company($record['layout_name'], $record['company_name']);
        }
    }
    
    if(hideempty == 'hide') {
        foreach($bannerArray as $bannerID => $records) {
            show_records($records);
        }
    } else {
        function show_banners(array $banners, array $bannerArray) {
            foreach($banners as $bannerID => $layoutInfo) {
                if(isset($bannerArray[$bannerID])) {
                    show_records($bannerArray[$bannerID]);
                } else {
                    layout_company($layoutInfo['layout_name'], 'empty');
                }
            }
        }
    
        $layoutGroupArray = mysqli_fetch_assoc(mysqli_query($link, "SELECT `set_layout_group` FROM `SetTopics` WHERE `set_id` = $setID"));
        $layoutGroup = $layoutGroupArray['set_layout_group'];
    
        show_banners($banners[0], $bannerArray);
        show_banners($banners[$layoutGroup], $bannerArray);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入