带有optlabels的动态php循环

trying to create a select list with labels. Everything with 0 belongs in the label tag with its child of listed below. Here is how my table is set up enter image description here

and the code I have which I know already is wrong

    $query = sprintf("SELECT * FROM `itemCat`");


$label = "";
while($row = $this->localDB->fetch_assoc($result))
{
    if ($row['itemCatName'] != $label && $row['itemCatChildof']=='0')
    {
    $cat .= '<optgroup label="'.$row['itemCatName'].'">';   
    }

    $cat .= '<option value="' . $row['iditemCat'] . '">' . $row['itemCatName'] . '</option>';
    $label =  $row['itemCatName'];
}
return $cat;
}

I would personnaly go for something like this : Creating a new sorted array where each parent element contain all his children in a sub array

<?php

$data = 
[
    ['iditemCat' => 1, 'itemCatName' => 'Admin', 'itemCatChildof' => 0],
    ['iditemCat' => 2, 'itemCatName' => 'Admin2', 'itemCatChildof' => 1],
    ['iditemCat' => 3, 'itemCatName' => 'Admin3', 'itemCatChildof' => 1],
    ['iditemCat' => 4, 'itemCatName' => 'Admin4', 'itemCatChildof' => 1],
    ['iditemCat' => 5, 'itemCatName' => 'Admin5', 'itemCatChildof' => 0],
    ['iditemCat' => 6, 'itemCatName' => 'Admin6', 'itemCatChildof' => 5],
    ['iditemCat' => 7, 'itemCatName' => 'Admin7', 'itemCatChildof' => 5],
    ['iditemCat' => 8, 'itemCatName' => 'Admin8', 'itemCatChildof' => 5]
];

$sortedArray = [];

foreach($data as $d) {
    if($d['itemCatChildof'] == 0) {
        $sortedArray[$d['iditemCat']] = $d;
    } else {
        $sortedArray[$d['itemCatChildof']]['children'][] = $d;
    }
}

It will return you something like this:

Array
(
    [1] => Array
        (
            [iditemCat] => 1
            [itemCatName] => Admin
            [itemCatChildof] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [iditemCat] => 2
                            [itemCatName] => Admin2
                            [itemCatChildof] => 1
                        )

                    [1] => Array
                        (
                            [iditemCat] => 3
                            [itemCatName] => Admin3
                            [itemCatChildof] => 1
                        )

                    [2] => Array
                        (
                            [iditemCat] => 4
                            [itemCatName] => Admin4
                            [itemCatChildof] => 1
                        )

                )

        )

    [5] => Array
        (
            [iditemCat] => 5
            [itemCatName] => Admin5
            [itemCatChildof] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [iditemCat] => 6
                            [itemCatName] => Admin6
                            [itemCatChildof] => 5
                        )

                    [1] => Array
                        (
                            [iditemCat] => 7
                            [itemCatName] => Admin7
                            [itemCatChildof] => 5
                        )

                    [2] => Array
                        (
                            [iditemCat] => 8
                            [itemCatName] => Admin8
                            [itemCatChildof] => 5
                        )

                )

        )

)

And so you just have to explore it like this:

<select name="foo" id="foo">
<?php foreach($sortedArray as $value): ?>
    <optgroup label="<?= $value['itemCatName']; ?>">
        <?php foreach($value['children'] as $child): ?>
            <option value="<?= $child['iditemCat']; ?>"><?= $child['itemCatName']; ?></option>
        <?php endforeach; ?>
    </optgroup>
<?php endforeach; ?>
</select>

Thank you to @Sakuto for pointing me in the right direction. Here is how it was solved

public function getAllCat(){
    $query = sprintf("SELECT * FROM `itemCat`");
        $result = $this->localDB->query($query);    
$cat = '<option value="">Please Select</option>';
$data = array();
    while($row = $this->localDB->fetch_assoc($result))
{
array_push($data, array('iditemCat'=>$row['iditemCat'], 'itemCatName'=>$row['itemCatName'],  'itemCatChildof'=>$row['itemCatChildof']));

}

$sortedArray = array();
foreach($data as $d) {
        if($d['itemCatChildof'] == 0) {
            $sortedArray[$d['iditemCat']] = $d;
        } else {
            $sortedArray[$d['itemCatChildof']]['children'][] = $d;
        }
    }


 foreach($sortedArray as $value){
   $cat .= '<optgroup label="'.$value['itemCatName'].'">';
    foreach($value['children'] as $child){
        $cat .='<option     value="'.$child['iditemCat'].'">'.$child['itemCatName'].'</option>';
    }
  $cat .=' </optgroup>';
 }

 return $cat;
}