You can group the result of subject_name into a PHP array by following the code.
$sql = "select * from users";
OR
$sql = "select subject_name,unit,unit_name from users";
PHP code
$result = mysqli_query($conn,$sql); $data = array(); function search($subject){ global $data; foreach ($data as $key => $value) { if (isset($value['subject_name']) && $value['subject_name']==$subject) { return $key; } } return false; } while($row = mysqli_fetch_assoc($result)){ $res = search($row['subject_name']); if ($res===false) { array_push($data, array( 'subject_name' =>$row['subject_name'], 'units'=>array($row) ) ); }else{ array_push($data[$res]['units'], $row); } } echo json_encode($data);
Now you can get your JSON format above the code.
source share