Stack Exchange
How to remove a row from an array using specific conditions in PHP
I have a simple array with some products to display in a html table.
array
Array
(
    [0] => Array
        (
            [item] => Hp-Probook
            [opening_qty] => 2
            [sold] => 10
            
        )

    [1] => Array
        (
             [item] => Hp-5420s
            [opening_qty] => 8
            [sold] => 4
        )

    [2] => Array
        (
            [item] => Dell-Inspiron
            [opening_qty] => 15
            [sold] => 10
        )

    [3] => Array
        (
            [item] => MacBook-Air-Pro
            [opening_qty] => 2
            [sold] => 10
        )
    [4] => Array
        (
            [item] => MacBook-Pro
            [opening_qty] => 2
            [sold] => 1
        )

)

Here I need To display some specified products separately for example mac-book series separately in table Like

|      name      |  qty  |  sold  |
-----------------------------------
|   Hp-Probook   |  2    |  10    |
|     Hp-5420s   |  8    |  4     |
|  Dell-Inspiron |  15   |  10    |
-----------------------------------
|    Total       |  25   |  24    |
-----------------------------------
|MacBook-Air-Pro |  5    |  5     |
|  MacBook-Pro   |  2    |  1     |
-----------------------------------
| total          |  7    |  6     |


I try to Unset the specific values from first foreach and after total row make next foreach
<?php
    $totalQty = 0;
    $totalSold = 0;
foreach ($product as $key=>$value) {
    if($value['item'] =='MacBook-Air-Pro'||$value['item'] =='MacBook-Pro') {
        unset($product[$key]);
    }

?>
    <tr>
        <td><?php echo $value['item'] ?></td>
        <td><?php echo $value['opening_qty'] ?></td>
        <td><?php echo $value['sold'] ?></td>
    </tr>
    <?php $totalQty +=  $value['opening_qty']; $totalSold += $value['sold'];?>
<?php } ?>
<tr>
    <td>Total</td>
    <td><?php echo $totalQty; ?></td>
    <td><?php echo $totalSold; ?></td>
</tr>

This display all Products Like normal foreach and the Unset even didn't work, confused to display the removed(unset) rows in next foreach.
is any alternative way to do this
Thanks,

First of all you needed continue not unset() (as unset deletes values, and what you want actually is to skip the record which meets the condition).
2nd you need to create another array and assign mac values to that array and then do the same process to show it:
<?php
    $totalQty = 0;
    $totalSold = 0;
    $macArray = [];
foreach ($product as $key=>$value) {
    if($value['item'] =='MacBook-Air-Pro'||$value['item'] =='MacBook-Pro') {
        $macArray[] = $value;
        continue;
    }

?>
    <tr>
        <td><?php echo $value['item'] ?></td>
        <td><?php echo $value['opening_qty'] ?></td>
        <td><?php echo $value['sold'] ?></td>
    </tr>
    <?php $totalQty +=  $value['opening_qty']; $totalSold += $value['sold'];?>
<?php } ?>
<tr>
    <td>Total</td>
    <td><?php echo $totalQty; ?></td>
    <td><?php echo $totalSold; ?></td>
</tr>

<?php 
    
    $totalQty = 0;
    $totalSold = 0;
    foreach($macArray as $value){?>
    <tr>
        <td><?php echo $value['item'] ?></td>
        <td><?php echo $value['opening_qty'] ?></td>
        <td><?php echo $value['sold'] ?></td>
    </tr>
    <?php $totalQty +=  $value['opening_qty']; $totalSold += $value['sold'];?>
<?php } ?>
<tr>
    <td>Total</td>
    <td><?php echo $totalQty; ?></td>
    <td><?php echo $totalSold; ?></td>
</tr>


unset really deletes the row.
The problem is that unset doesn't stop the current iteration of the foreach loop, so even if the row is deleted, it will output the product line.
In your case you can try to replace unset($product[$key]); by continue;
e.g.
<?php
    $totalQty = 0;
    $totalSold = 0;
foreach ($product as $key=>$value) {
    if($value['item'] =='MacBook-Air-Pro'||$value['item'] =='MacBook-Pro') {
        continue;
    }

?>
    <tr>
        <td><?php echo $value['item'] ?></td>
        <td><?php echo $value['opening_qty'] ?></td>
        <td><?php echo $value['sold'] ?></td>
    </tr>
    <?php $totalQty +=  $value['opening_qty']; $totalSold += $value['sold'];?>
<?php } ?>
<tr>
    <td>Total</td>
    <td><?php echo $totalQty; ?></td>
    <td><?php echo $totalSold; ?></td>
</tr>


good catch! I am 5 second late to catch that
