foreach循环中的PHP Unset Array对象

时间:2016-12-20 03:39:16

标签: php arrays codeigniter loops foreach

我在unset循环中尝试foreach 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 
    } ?>

以上代码显示的结果如下图所示:

enter image description here

我希望unset每个已打印的对象的数据包装为4行(请查看下面的屏幕截图)。我已将unset($summaryResult)放在最后&#39; foreach&#39;循环,但它不起作用。

enter image description here

任何帮助都会被贬低。

3 个答案:

答案 0 :(得分:0)

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

请尝试更改    从

    foreach($summaryResult as $rows)

    foreach($summaryResult as &$rows)

有关详情,请点击链接 - http://www.php.net/manual/en/control-structures.foreach.php

  

为了能够直接修改循环内的数组元素   使用&amp;

在$ value之前

答案 1 :(得分:0)

假设你的阵列是这样的;

$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)以创建表格。

也许有人可以简化这个过程并为你修复这个答案。

希望得到这个帮助。

答案 2 :(得分:0)

您可以使用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数据有一个模式时,我会建议为它创建一个函数,所以没有代码重复。