对每个循环使用相同的代码,以便有效地进行循环。

时间:2022-10-18 22:06:57

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?

我有一些不同的数据集通过foreach循环。每个foreach循环中的许多代码是相似的,几乎是相同的,但是我使用的变量是基于先前定义的数组以及放在foreach循环中的初始数据。有没有一种方法可以将这些循环组合成某种函数而不需要将所有数据传递到函数中?

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 个解决方案

#1


1  

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);
}

#2


2  

This should do the trick, its your code refactored into 2 functions, one for creating single rows and one for looping through your arrays to create multiple.

这应该很有用,它将代码重构为两个函数,一个用于创建单行,另一个用于循环遍历数组以创建多个。

 <?php
    function createTableRow($layout_name, $company_name){
        $tablerow = "
            <tr>
                <td>".$layout_name."</td>
                <td>".$company_name."</td>
            </tr>   
        ";
        return $tablerow
    }

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

 ?>

#3


1  

you can refactor it to use a helper function for echoing the tr tag, e.g.

您可以重构它来使用一个帮助函数来响应tr标记,例如。

function displayTableRow($layout, $company) {

echo "<tr>
    <td>$layout</td>
    <td>$company</td>
</tr>";
}

and use this in your foreach loops like:

在你的foreach循环中使用这个

      if(isset($bannerArray[$bannerID])){
          foreach($bannerArray[$bannerID] as $id => $record){ 
              displayTableRow($record['layout_name'], $record['company_name']);
          }
       } else {
              displayTableRow($record['layout_name'], 'empty');
          }
       }

#4


0  

Would be something like this (NOT TESTED!)

会是这样(没有测试!)

<?php
function getTableRecords ($array, $condition = NULL) {
    foreach($array as $key => $value) {
        if (!is_null ($condition)) { 
            foreach($value as $subKey => $subValue) {
                $result = "<tr><td>" .  
                           $subValue['layout_name']  . "</td><td>"  .  
                           $subValue['company_name'] . "</td></tr>";
            }
        }
        else {
            return "<tr>
                        <td>" . $value['layout_name'] . "</td>
                        <td>empty</td>
                    </tr>";
        }
    }
}

if($hideempty == 'hide'){
    print getTableRecords ($bannerArray);
} else {
    $layoutGroupArray = mysqli_fetch_assoc(mysqli_query($link, "SELECT `set_layout_group` FROM `SetTopics` WHERE `set_id` = $setID"));
    $layoutGroup = $layoutGroupArray['set_layout_group'];
    print getTableRecords ($banners[0],
                        isset ($bannerArray[$bannerID]) ? $bannerArray[$bannerID] : NULL);
    print getTableRecords ($banners[$layoutGroup],
                        isset ($bannerArray[$bannerID]) ? $bannerArray[$bannerID] : NULL);
}

#1


1  

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);
}

#2


2  

This should do the trick, its your code refactored into 2 functions, one for creating single rows and one for looping through your arrays to create multiple.

这应该很有用,它将代码重构为两个函数,一个用于创建单行,另一个用于循环遍历数组以创建多个。

 <?php
    function createTableRow($layout_name, $company_name){
        $tablerow = "
            <tr>
                <td>".$layout_name."</td>
                <td>".$company_name."</td>
            </tr>   
        ";
        return $tablerow
    }

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

 ?>

#3


1  

you can refactor it to use a helper function for echoing the tr tag, e.g.

您可以重构它来使用一个帮助函数来响应tr标记,例如。

function displayTableRow($layout, $company) {

echo "<tr>
    <td>$layout</td>
    <td>$company</td>
</tr>";
}

and use this in your foreach loops like:

在你的foreach循环中使用这个

      if(isset($bannerArray[$bannerID])){
          foreach($bannerArray[$bannerID] as $id => $record){ 
              displayTableRow($record['layout_name'], $record['company_name']);
          }
       } else {
              displayTableRow($record['layout_name'], 'empty');
          }
       }

#4


0  

Would be something like this (NOT TESTED!)

会是这样(没有测试!)

<?php
function getTableRecords ($array, $condition = NULL) {
    foreach($array as $key => $value) {
        if (!is_null ($condition)) { 
            foreach($value as $subKey => $subValue) {
                $result = "<tr><td>" .  
                           $subValue['layout_name']  . "</td><td>"  .  
                           $subValue['company_name'] . "</td></tr>";
            }
        }
        else {
            return "<tr>
                        <td>" . $value['layout_name'] . "</td>
                        <td>empty</td>
                    </tr>";
        }
    }
}

if($hideempty == 'hide'){
    print getTableRecords ($bannerArray);
} else {
    $layoutGroupArray = mysqli_fetch_assoc(mysqli_query($link, "SELECT `set_layout_group` FROM `SetTopics` WHERE `set_id` = $setID"));
    $layoutGroup = $layoutGroupArray['set_layout_group'];
    print getTableRecords ($banners[0],
                        isset ($bannerArray[$bannerID]) ? $bannerArray[$bannerID] : NULL);
    print getTableRecords ($banners[$layoutGroup],
                        isset ($bannerArray[$bannerID]) ? $bannerArray[$bannerID] : NULL);
}