2
我試圖從同一個表中獲取父類別和子類別。我正在使用php rest api。如何使用php mysql獲取嵌套的json對象
這是我的桌子。
id parent category
--------------------
1 | 0 | Fruits
2 | 0 | Cars
3 | 1 | Orange
4 | 1 | Apple
5 | 0 | Books
6 | 2 | Benz
7 | 5 | Comics
8 | 2 | Honda
9 | 5 | Fantasy
我怎麼能得到下面的json php/mysql的查詢。
{
"categories": [{
"id": "1",
"category": "Fruits",
"subCat": [{
"id": "3",
"category": "Orange"
}, {
"id": "4",
"category": "Apple"
}]
}, {
"id": "2",
"category": "Cars",
"subCat": [{
"id": "6",
"category": "Benz"
}, {
"id": "8",
"category": "Honda"
}]
}, {
"id": "5",
"category": "Books",
"subCat": [{
"id": "7",
"category": "Comics"
}, {
"id": "9",
"category": "Fantasy"
}]
}
]
}
感謝您的任何幫助。
答案是如此的有用,只是希望數組轉換工作 –