More Answer here...
How about if you have more than 1 parent category
like this

Use this query
SELECT `parent_category`.`id`, `parent_category`.`name` as `parent_category`, `child_category`.`name` as `child_category` FROM `category` `parent_category` join `category` `child_category` on `parent_category`.`id` = `child_category`.`parent_id`
This query produce result like this

To group your category first
declare a variable which contains the parent_id of your $row
$prev_parent = null;
let it be as null
then
in while loop
Create 3 condition(s)
First Condition: check if $prev_parent is null then create your first category
if ($prev_parent == null) {
$prev_parent = $row['id'];
$listinfdata[] = array(
'name' => $row['parent_category'],
'subcat' => array($row['child_category'])
);
}
This condition would create something like this
Array
(
[0] => Array
(
[name] => 'Parent Category 1'
[subcat] => Array
(
[0] => 'Child Category 1'
)
)
);
Second Condition: check if $row['id'] or parent_id is same as $prev_parent which your your previous parent id category
If that match to this condition just insert the subcategory to the latest subcategories of the latest parent category
`sizeof($listinfdata)-1` since array always started at index 0
Do this
$prev_parent = $row['id'];
$listinfdata[sizeof($listinfdata)-1]['subcat'][] = $row['child_category'];
This condition would create something like this
Array
(
[0] => Array
(
[name] => 'Parent Category 1'
[subcat] => Array
(
[0] => 'Child Category 1',
[1] => 'Child Category 2'
)
)
);
Last Condition else means if $prev_parent is not empty but the current parent_category is not same to the
$prev_parent insert another parent category with it's first sub category
$prev_parent = $row['id'];
$listinfdata[] = array(
'name' => $row['parent_category'],
'subcat' => array($row['child_category'])
);
This condition would create something like this
Array
(
[0] => Array
(
[name] => 'Parent Category 1'
[subcat] => Array
(
[0] => 'Child Category 1',
[1] => 'Child Category 2'
)
),
[1] => Array
(
[name] => 'Parent Category 2'
[subcat] => Array
(
[0] => 'Child Category 1',
)
)
);
So your code would be like this
<?php
$category = [];
$sub_category = [];
$query_category = "SELECT `parent_category`.`id`, `parent_category`.`name` as `parent_category`, `child_category`.`name` as `child_category` FROM `category` `parent_category` join `category` `child_category` on `parent_category`.`id` = `child_category`.`parent_id`";
$sub_category = $this->runSelectQuery($query_category);
return array($sub_category);
function `runSelectQuery` is like...
public function runSelectQuery($sql) {
$result = mysqli_query($this->con, $sql);
$listinfdata = array();
$prev_parent = null;
while ($row = mysqli_fetch_assoc($result)) {
$category = null;
if ($prev_parent == null) {
$prev_parent = $row['id'];
$listinfdata[] = array(
'name' => $row['parent_category'],
'subcat' => array($row['child_category'])
);
} else if ($row['id'] == $prev_parent) {
$prev_parent = $row['id'];
$listinfdata[sizeof($listinfdata)-1]['subcat'][] = $row['child_category'];
} else {
$prev_parent = $row['id'];
$listinfdata[] = array(
'name' => $row['parent_category'],
'subcat' => array($row['child_category'])
);
}
}
return $listinfdata;
}
And the result is like this

select name from category where parent_id in (0, 1)