Anda bisa melakukan ini dengan kombinasi PHP dan MySQL. Ubah kueri Anda menjadi ini:
SELECT section_titel as t1, GROUP_CONCAT(sub_section_titel) as t2
FROM sections LEFT JOIN sub_sections ON section_id = sId
GROUP BY t1
HAVING t2 IS NOT NULL
Ini akan memberi Anda tabel hasil seperti ini:
t1 t2
Section One SubOne,SubTwo
Section Three SubThree
(Jika Anda menginginkan hasil untuk Section Two
, hapus HAVING t2 IS NOT NULL
kondisi dari kueri)
Kemudian di PHP Anda (saya berasumsi mysqli
dengan koneksi $conn
)
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$out = array();
while ($row = mysqli_fetch_array($result)) {
$out[] = array('t1' => $row['t1'], 't2' => explode(',', $row['t2']));
}
print_r($out);
Keluaran:
Array
(
[0] => Array
(
[t1] => Section One
[t2] => Array
(
[0] => SubOne
[1] => SubTwo
)
)
[1] => Array
(
[t1] => Section Three
[t2] => Array
(
[0] => SubThree
)
)
)