How to show in another table how many value of 1,2,3 from “kondisi” column in each cluster?

This image is the example of the dataset

So i’ve asked this to ChatGPT and another AI helper, but the result is 0.

This image is table to show what i want

So from the image about table to show what i want, column “Baik” means how many number 1 is from column “kondisi” in the dataset, same as “sedang”: 2 ; “buruk”: 3;

And this below is the code that i try to fix because the result is 0 :

<h4>Jumlah Cluster</h4>
<table class="table table-border">
  <thead>
    <th>Cluster</th>
    <th>Jumlah</th>
    <th>Baik</th>
    <th>Sedang</th>
    <th>Buruk</th>
  </thead>
  <tbody>
    <?php
    if ($this->session->userdata("kmeans_result") !== NULL) {
      $res = array();
      $kondisiCounts = array();
      foreach ($this->session->userdata("kmeans_result") as $key) {
        if (!isset($res[$key[1]])) {
          $res[$key[1]] = 1;
        } else {
          $res[$key[1]]++;
        }

        // Check if 'kondisi' key exists and access it safely
        $kondisi = $key['kondisi'] ?? 0;

        // Count Kondisi values
        $clusterId = $key[1];
        if (!isset($kondisiCounts[$clusterId])) {
          $kondisiCounts[$clusterId] = array('Baik' => 0, 'Sedang' => 0, 'Buruk' => 0);
        }
        // Increment the count based on the Kondisi value
        if ($kondisi === 1) {
          $kondisiCounts[$clusterId]['Baik']++;
        } elseif ($kondisi === 2) {
          $kondisiCounts[$clusterId]['Sedang']++;
        } elseif ($kondisi === 3) {
          $kondisiCounts[$clusterId]['Buruk']++;
        }
      }

      foreach ($res as $key => $val) {
        ?>
        <tr>
          <td><?=$key?></td>
          <td><?=$val?></td>
          <td><?=$kondisiCounts[$key]['Baik'] ?? 0?></td>
          <td><?=$kondisiCounts[$key]['Sedang'] ?? 0?></td>
          <td><?=$kondisiCounts[$key]['Buruk'] ?? 0?></td>
        </tr>
        <?php
      }
    }
    ?>
  </tbody>
</table>

The dataset is from session : $this->session->userdata(“kmeans_result”)

And this below is code to show the result and become the dataset in the first image, and i use this code for reference when i ask to AI, but their solution still give result 0 in each column :

<h4>Hasil Cluster K-Means</h4>
                <div class="table-responsive" id="export">
                  <table class="table table-border">
                    <thead>
                      <?php
                        foreach ($this->session->userdata("process_datasetindex") as $n => $v) {
                          if($n==0){
                            $obj = $v;
                          }
                          ?>
                          <th><?=$v?></th>
                          <?php
                        }
                      ?>
                      <th>Cluster</th>
                    </thead>
                    <?php
                    if($this->session->userdata("kmeans_result")!==NULL){
                      $resk = $this->session->userdata("kmeans_result");
                      aasort($resk,1);
                      foreach ($resk as $key) {
                        ?>
                        <tr>
                          <td><?=$key[0]?></td>
                          <?php
                           foreach ($this->session->userdata("process_datasetindex") as $n => $v) {
                             if($n>0){
                               $attr = array_column($this->session->userdata("process_dataset"),$v,$obj);
                               ?>
                               <td><?=$attr[$key[0]]?></td>
                               <?php
                             }

                           }
                          ?>
                          <td><?=$key[1]?></td>
                        </tr>
                        <?php
                      }
                    }
                    ?>
                  </table>