【问题标题】:PHP Unset Array Object in foreach LoopPHP在foreach循环中取消设置数组对象
【发布时间】:2017-05-05 05:02:32
【问题描述】:

我正在尝试在foreach 循环中使用unset stdClass 对象。这是我的代码:

<?php
    if (isset($summaryResult)) {
        foreach($summaryResult as $rows) { ?>
            <tr>
                <td><?php echo $rows->st_date;?></td>
                <td>
                  <?php 
                      if(($rows->st_time_09 == '')&&($rows->st_time_11 == '')&&($rows->st_time_13 == '')){echo $rows->st_time_07;}
                      elseif(($rows->st_time_07 == '')&&($rows->st_time_11 == '')&&($rows->st_time_13 == '')){echo $rows->st_time_09;}
                      elseif(($rows->st_time_07 == '')&&($rows->st_time_09 == '')&&($rows->st_time_13 == '')){echo $rows->st_time_11;}
                      elseif(($rows->st_time_07 == '')&&($rows->st_time_09 == '')&&($rows->st_time_11 == '')){echo $rows->st_time_13;}
                      else{}
                    ?>                  
                </td>
                <td>
                  <?php 
                      if(($rows->st_chlorine_09 == '')&&($rows->st_chlorine_11 == '')&&($rows->st_chlorine_13 == '')){echo $rows->st_chlorine_07;}
                      elseif(($rows->st_chlorine_07 == '')&&($rows->st_chlorine_11 == '')&&($rows->st_chlorine_13 == '')){echo $rows->st_chlorine_09;}
                      elseif(($rows->st_chlorine_07 == '')&&($rows->st_chlorine_09 == '')&&($rows->st_chlorine_13 == '')){echo $rows->st_chlorine_11;}
                      elseif(($rows->st_chlorine_07 == '')&&($rows->st_chlorine_09 == '')&&($rows->st_chlorine_11 == '')){echo $rows->st_chlorine_13;}
                      else{}
                    ?>  
                </td>
                <td>
                  <?php 
                      if(($rows->st_time_15 == '')&&($rows->st_time_17 == '')&&($rows->st_time_19 == '')){echo $rows->st_time_21;}
                      elseif(($rows->st_time_21 == '')&&($rows->st_time_17 == '')&&($rows->st_time_19 == '')){echo $rows->st_time_15;}
                      elseif(($rows->st_time_21 == '')&&($rows->st_time_15 == '')&&($rows->st_time_19 == '')){echo $rows->st_time_17;}
                      elseif(($rows->st_time_21 == '')&&($rows->st_time_15 == '')&&($rows->st_time_17 == '')){echo $rows->st_time_19;}
                      else{}
                    ?>  
                </td>
                <td>
                  <?php 
                      if(($rows->st_chlorine_21 == '')&&($rows->st_chlorine_17 == '')&&($rows->st_chlorine_19 == '')){echo $rows->st_chlorine_15;}
                      elseif(($rows->st_chlorine_21 == '')&&($rows->st_chlorine_15 == '')&&($rows->st_chlorine_19 == '')){echo $rows->st_chlorine_17;}
                      elseif(($rows->st_chlorine_21 == '')&&($rows->st_chlorine_15 == '')&&($rows->st_chlorine_17 == '')){echo $rows->st_chlorine_19;}
                      elseif(($rows->st_chlorine_15 == '')&&($rows->st_chlorine_17 == '')&&($rows->st_chlorine_19 == '')){echo $rows->st_chlorine_21;}
                      else{}
                  ?>
                </td>
                <td>
                  <?php 
                      if(($rows->st_time_23 == '')&&($rows->st_time_01 == '')&&($rows->st_time_03 == '')){echo $rows->st_time_05;}
                      elseif(($rows->st_time_05 == '')&&($rows->st_time_01 == '')&&($rows->st_time_03 == '')){echo $rows->st_time_23;}
                      elseif(($rows->st_time_05 == '')&&($rows->st_time_23 == '')&&($rows->st_time_03 == '')){echo $rows->st_time_01;}
                      elseif(($rows->st_time_05 == '')&&($rows->st_time_23 == '')&&($rows->st_time_01 == '')){echo $rows->st_time_03;}
                      else{}
                  ?>
                </td>
                <td>
                  <?php 
                      if(($rows->st_chlorine05 == '')&&($rows->st_chlorine01 == '')&&($rows->st_chlorine03 == '')){echo $rows->st_chlorine23;}
                      elseif(($rows->st_chlorine05 == '')&&($rows->st_chlorine23 == '')&&($rows->st_chlorine03 == '')){echo $rows->st_chlorine01;}
                      elseif(($rows->st_chlorine05 == '')&&($rows->st_chlorine23 == '')&&($rows->st_chlorine01 == '')){echo $rows->st_chlorine03;}
                      elseif(($rows->st_chlorine23 == '')&&($rows->st_chlorine01 == '')&&($rows->st_chlorine03 == '')){echo $rows->st_chlorine05;}
                      else{}
                  ?>
                </td>
            </tr>
        <?php 
        } 


        // unset($summaryResult);  <== why this unset doesn't work ?
    } else { ?>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    <?php 
    } ?>

上面的代码是显示如下截图的结果:

我希望 unset 每个已打印的对象将数据包装成 4 行(检查下面的屏幕截图)。我已将 unset($summaryResult) 放在“foreach”循环的末尾,但它不起作用。

任何帮助都将不胜感激。

【问题讨论】:

  • 你未设置的不在foreach里面
  • 如果在foreach内部没有设置,运行一组数组后会停止。据我了解,@metaphor 尽量不要打印空白单元格
  • 你不能在这里“取消设置”一些东西。您需要重新构建此文件的整个结构,以获得所需的表格数据显示。
  • @weirdo 尝试不打印空白单元格 怎么办?我试过使用array_diffarray_filter。顺便说一句,这些字段是String。是否可以根据上述数据过滤掉空字符串
  • 处理它.. 但这很难,因为您的数据可能在您的数组中随机出现。

标签: php arrays codeigniter loops foreach


【解决方案1】:

如果我理解正确,您想在 foreach 循环内修改(取消设置)数组吗?尝试在值之前添加 &

请尝试更改 来自

    foreach($summaryResult as $rows)

    foreach($summaryResult as &$rows)

欲了解更多信息,请点击链接 - http://www.php.net/manual/en/control-structures.foreach.php

为了能够在循环内直接修改数组元素 $value 前加 &

【讨论】:

  • 我可以简单地说我只想重新排序数据而不打印空白字段
【解决方案2】:

假设你的数组是这样的;

$arr = array (
        array(  "time" => "30/06/2016", 
                "time1" => 11,
                "shift1" => 1,
                "time2"  => "",
                "shift2" => "",
                "time3"  => "",
                "shift3" => "",
                "time4"  => 6,
                "shift4" => 2
             ),
        array(  "time" => "30/06/2016", 
                "time1" => "",
                "shift1" => "",
                "time2"  => 2,
                "shift2" => 1,
                "time3"  => 3,
                "shift3" => 1,
                "time4"  => "",
                "shift4" => ""
             ),
        array(  "time" => "30/06/2016", 
                "time1" => "",
                "shift1" => "",
                "time2"  => 12,
                "shift2" => 1,
                "time3"  => "",
                "shift3" => "",
                "time4"  => 5,
                "shift4" => 3
             ),
        array(  "time" => "30/06/2016", 
                "time1" => 12,
                "shift1" => 0.9,
                "time2"  => "",
                "shift2" => "",
                "time3"  => 3,
                "shift3" => 1,
                "time4"  => "",
                "shift4" => ""
             )

    );

垂直读取数组,只抓取有值的数组;

foreach ($arr as $sub_arr)
{
    if ($sub_arr["time1"]) $new_arr["time1"][] = $sub_arr["time1"];
    if ($sub_arr["shift1"]) $new_arr["shift1"][] = $sub_arr["shift1"];

    if ($sub_arr["time2"]) $new_arr["time2"][] = $sub_arr["time2"];
    if ($sub_arr["shift2"]) $new_arr["shift2"][] = $sub_arr["shift2"];

    if ($sub_arr["time3"]) $new_arr["time3"][] = $sub_arr["time3"];
    if ($sub_arr["shift3"]) $new_arr["shift3"][] = $sub_arr["shift3"];

    if ($sub_arr["time4"]) $new_arr["time4"][] = $sub_arr["time4"];
    if ($sub_arr["shift4"]) $new_arr["shift4"][] = $sub_arr["shift4"];
}

重构数组以满足您的目的。假设垂直读取后的数组大小相同。

$count = count($new_arr["time1"]); // assume same size base on your data
for ($i=0; $i<$count;++$i)
{
    $mergArr[] = array(     "time1" => $new_arr["time1"][$i],
                            "shift1" => $new_arr["shift1"][$i],
                            "time2"  => $new_arr["time2"][$i],
                            "shift2" => $new_arr["shift2"][$i],
                            "time3"  => $new_arr["time3"][$i],
                            "shift3" => $new_arr["shift3"][$i],
                            "time4"  => $new_arr["time4"][$i],
                            "shift4" => $new_arr["shift4"][$i]
                         );
}

最后,将您的foreach($summaryResult as $rows) 替换为foreach($mergArr as $rows) 以创建您的表格。

也许有人可以简化流程并为您修复此答案。

希望对您有所帮助。

【讨论】:

    【解决方案3】:

    您可以使用array_reducearray_mergearray_filter 预先将数组合并在一起:

    $result = array_reduce($summaryResult, function ($carry, $item) {
        $carry[$item['st_date']] = isset($carry[$item['st_date']]) 
            ? array_merge($carry[$item['st_date']], array_filter($item, 'strval'))
            : $item;
    
        return $carry;
    }, []);
    

    另外,当你echo 数据有一个模式时,我建议为此创建一个函数,所以没有代码重复。

    【讨论】:

      猜你喜欢
      • 2019-04-13
      • 2013-10-23
      • 2011-02-20
      • 2019-05-11
      • 2011-01-01
      • 2015-06-02
      • 2011-03-04
      • 1970-01-01
      • 2012-05-02
      相关资源
      最近更新 更多