2
votes

I'm trying to unset stdClass object inside foreach loop. Here is my code:

<?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 
    } ?>

Above code is display the result like the following screenshot:

enter image description here

I want the data wrapping to 4 lines (check below screenshot) by unset every object which already printed. I've put unset($summaryResult) at the end 'foreach' loop but it doesn't work.

enter image description here

Any help would be appreaciated.

3
you unset is not inside the foreachLF00
if unset inside the foreach, it will be stop after run one set of array. From what I'm understand, @metaphor try not to print a blank cellweirdo
You cannot just "unset" something here. Your whole structure of this file needs to be rebuilded for you to get desired table data display.Joe May
@weirdo try no to print a blank cell how to do it ? i've tried using array_diff and array_filter. Btw, those fields are String. Is it possible to filter out the empty strings only according to above data ?metaphor
work on it.. but it hard because your data could be randomly inside your array.weirdo

3 Answers

0
votes

If I correct understand, you want do modify (unset) array inside of foreach loop ? Try to add & before value

Please try to change from

    foreach($summaryResult as $rows)

to

    foreach($summaryResult as &$rows)

For more info please follow link - http://www.php.net/manual/en/control-structures.foreach.php

In order to be able to directly modify array elements within the loop precede $value with &

0
votes

Assume your array like this;

$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" => ""
             )

    );

Vertical reading of array and grab only array with value;

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"];
}

reconstruct array to meet your purpose. Assume your array size after vertical reading will be same.

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

And lastly, replace your foreach($summaryResult as $rows) with foreach($mergArr as $rows) to create your table.

Maybe somebody can simplified the process and repair this answer for you.

Hope this help.

0
votes

You can merge arrays together beforehand using array_reduce, array_merge and array_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;
}, []);

Also, when you echo data there is a pattern, I would suggest creating a function for that, so there's no code duplication.