'combine data if same value php, html table, codeigniter 3

I will create a table with data in and out of goods in 1 month,i use codeigniter 3, i got data by join sql. and the result is:

array from sql

and i implement to html table with:

<table class="table table-hover table-inverse table-responsive">
    <thead class="thead-inverse">
      <tr>
        <th rowspan="3">Nama barang</th>
        <th colspan="<?= $number * 2 ?>">Bulan <?php echo $dt->format("m"); ?></th>
      </tr>
      <tr>
        <?php for ($i = 1; $i <= $number; $i++) { ?>

          <th colspan="2"> <?php echo $i ?></th>
        <?php } ?>
      </tr>
      <tr>
        <?php for ($i = 1; $i <= $number; $i++) { ?>
          <td>in</td>
          <td>out</td>
        <?php } ?>
      </tr>
    </thead>
    <tbody>
      <?php foreach ($barang_keluar as $barang_keluar) {
        $day = date_create($barang_keluar['tanggal_keluar']);
      ?>
        <tr>
          <td><?= $barang_keluar['barang'] ?></td>
          <?php for ($i = 1; $i <= $number * 2; $i++) { ?>
            <?php $tanggal = $day->format('d');
            if ($i == $tanggal * 2 - 1) { ?>
              <td style="text-align: center; background-color: red; color: white;"><?= $barang_keluar['masuk'] ?></td>
              <?php if ($i == $tanggal * 2 - 1) { ?>
                <td style="text-align: center; background-color: violet; color: white;"><?= $barang_keluar['keluar'] ?></td>
              <?php   } else { ?>
                <td></td>
              <?php } ?>
            <?php } else { ?>
              <td></td>
            <?php }
            ?>
          <?php } ?>
        </tr>
      <?php } ?>
    </tbody>
  </table>

and i have the value like: enter image description here

the question is: how to make the "pulpen" data is one row like: enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source