$parent = []; $child = []; foreach ($subjects as $k => $v) { if (0 == $v['pid']) { $parent[] = $v; } else { $child[$v['pid']][] = $v; } }
<?php foreach ($parent as $k => $v): ?> <tr> <td><input name="major_subject[]" type="checkbox" value="<?= $v['id']; ?>" οnclick="sele(this)" att="at_<?= $v['id']; ?>"/><?= $v['name']; ?></td> <td> <table class="fpqx_table_gxdq" width="100%" cellspacing="0" cellpadding="0" border="0"> <tbody> <?php foreach ($child[$v['id']] as $kk => $vv): ?> <tr> <td><input type="checkbox" value="<?= $vv['id']; ?>" att="at_<?= $v['id']; ?>" at="at_<?= $vv['id']; ?>" οnclick="sele(this)" name="major_subject[]"/><?= $vv['name']; ?></td> <?php foreach ($child[$vv['id']] as $kkk => $vvv) : ?> <td><input type="checkbox" value="<?= $vvv['id']; ?>" att="at_<?= $v['id']; ?>" at="at_<?= $vv['id']; ?>" name="major_subject[]"><?= $vvv['name']; ?></td> <?php endforeach; ?> </tr> <?php endforeach; ?> </tbody> </table> </td> </tr> <?php endforeach; ?>