0
我在php和codeigniter ramework環境中創建類別樹結構時遇到問題。php codeigniter中樹數據的算法
所以現在我想要的數據結構。
數據舉例:
{
"category": [
{
"id" : "1",
"text" : "test1"
"children": false,
}, {
"id" : "2",
"text" : "test2",
"children": [
{
"children": false,
"id" : "3",
"text" : "test3"
}, {
"children": false,
"id" : "4",
"text" : "test4"
}, {
"children": false,
"id" : "5",
"text" : "test5"
}, {
"children": false,
"id" : "6",
"text" : "test7"
}
]
}
]
}
我無法創建每個JSON對象的兒童
查詢我想一個數組:
public function setCategoryTree($categoryUp = null)
{
$param = array();
$query = ' SELECT id, name, categoryUp
FROM categoryTable
WHERE categoryUp = ? ';
return $this->commerce_db->query($query)->result();
}
控制器我想到:
public function getCategoryTree(){
$this->load->model('category_m');
$result = $this->category_m->setCategoryTree();
$resultData = array();
foreach($result as $value) {
$treeTmpData = array();
$treeTmpData['id'] = $value->id;
$treeTmpData['text'] = $value->text;
$treeTmpData['children'] = ????
$resultData[] = $treeTmpData;
}
echo json_encode($resultData, JSON_UNESCAPED_UNICODE);
}
祝我好運
請提供categoryTable的結構。 –