我正在使用一個PHP類,它從MySQL數據庫生成json格式的嵌套類別樹菜單。問題是,該陣列用於填充數據沒有做它應該PHP Array奇怪的行爲
PHP類是:
class nestedCategories {
public $json = array();
public function generateMenu($categories,$level){
$this->json = array();
foreach($categories as $category){
$this->json['title'] = $category->description;
$this->json['expanded'] = true;
if(isset($category->children) && count($category->children)>0){
$this->json['folder'] = true;
$this->json['children'] = $this->generateMenu($category->children,$category->category_id);
} else {
$this->json['folder'] = false;
}
}
}
public function getJSON() {
return $this->json;
}
的$ JSON是應該看起來像:
[
{"title": "Animalia", "expanded": true, "folder": true, "children": [
{"title": "Chordate", "folder": true, "children": [
{"title": "Mammal", "children": [
{"title": "Primate", "children": [
{"title": "Primate", "children": [
]},
{"title": "Carnivora", "children": [
]}
]},
{"title": "Carnivora", "children": [
{"title": "Felidae", "lazy": true}
]}
]}
]},
{"title": "Arthropoda", "expanded": true, "folder": true, "children": [
{"title": "Insect", "children": [
{"title": "Diptera", "lazy": true}
]}
]}
]}
]
但是,當我試圖從MySQL提取數據,在$ JSON數組只給我從MySQL最後拉數據,如果是刪除所有數據背後
有人能說服我低谷?
您錯過了您提供的課程中關閉班級的最後一個支架。 – 2014-12-02 17:15:52
@battletoilet:如果這是一個問題,代碼甚至不會運行。 – 2014-12-02 17:16:15
當你執行'$ this-> json ['title'] = $ category-> description;'在循環內? – 2014-12-02 17:16:47