我正在使用一个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