2014-12-02 148 views
0

我正在使用一个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最后拉数据,如果是删除所有数据背后

有人能说服我低谷?

+0

您错过了您提供的课程中关闭班级的最后一个支架。 – 2014-12-02 17:15:52

+0

@battletoilet:如果这是一个问题,代码甚至不会运行。 – 2014-12-02 17:16:15

+1

当你执行'$ this-> json ['title'] = $ category-> description;'在循环内? – 2014-12-02 17:16:47

回答

2

要覆盖阵列每个循环周围categories,并且每次都使用数组赋值清除成员变量。

另外,我怀疑它不会返回你要找的,除非你让它preoperly递归结构(这意味着返回你已经建立了阵列):

class nestedCategories { 

    public function generateMenu($categories,$level){ 

     $categoriesJson = array(); 

     foreach($categories as $category){ 

      $categoryJson = array(); 
      $categoryJson['title'] = $category->description; 
      $categoryJson['expanded'] = true; 

      if(isset($category->children) && count($category->children)>0){ 
       $categoryJson['folder'] = true; 
       $categoryJson['children'] = $this->generateMenu($category->children,$category->category_id); 
      } else { 
       $categoryJson['folder'] = false;   
      } 
      $categoriesJson[] = $categoryJson; 
     } 

     return $categoriesJson; 
    } 
} 

现在,您会通过调用generateMenu而不是使用成员变量来取回数据。

+0

谢谢!在我急于节省时间的情况下,我错过了逻辑。 – rosuandreimihai 2014-12-02 17:27:30

1

您在generateMenu方法的顶部设置$this->json = array();。当你进行递归调用时,它将销毁前一次调用中写入数组的内容。

另外在每次递归调用$this->json['title']都会写上以前的标题。 expanded,childrenfolder也是如此。

它看起来像我花了一点时间,我应该做的示例代码。无论如何,这是它。

class Foo { 
    public function generateMenu($categories){ 
     $json_array = array(); 
     foreach($categories as $category) { 
      $json_object = array(); 
      $json_object['title'] = $category->description; 

      if(isset($category->children) && count($category->children)>0){ 
       // it looks like expanded should only be 
       // in objects with > 0 children 
       $json_object['expanded'] = true; 
       $json_object['folder'] = true; 
       $json_object['children'] = $this->generateMenu($category->children); 

      } else { 
       // based on the json in the question it looks like 
       // it should always contain a children array, but no folder element 
       $json_object['children'] = array();   
      } 
      $json_array[] = $json_object; 
     } 
     return $json_array; 
    } 
} 

下面是测试数据:

$Diptera = new stdClass(); 
$Diptera->description = 'Diptera'; 

$Insect = new stdClass(); 
$Insect->description = 'Insect'; 
$Insect->children = array($Diptera); 

$Arthropoda = new stdClass(); 
$Arthropoda->description = 'Arthropoda'; 
$Arthropoda->children = array($Insect); 

$Felidae = new stdClass(); 
$Felidae->description = 'Felidae'; 

$Carnivora2 = new stdClass(); 
$Carnivora2->description = 'Carnivora'; 
$Carnivora2->children = array($Felidae); 

$Carnivora = new stdClass(); 
$Carnivora->description = 'Carnivora'; 

$Primate2 = new stdClass(); 
$Primate2->description = 'Primate'; 

$Primate = new stdClass(); 
$Primate->description = 'Primate'; 
$Primate->children = array($Primate2, $Carnivora); 

$Mammal = new stdClass(); 
$Mammal->description = 'Mammal'; 
$Mammal->children = array($Primate, $Carnivora2); 

$Chordate = new stdClass(); 
$Chordate->description = 'Chordate'; 
$Chordate->children = array($Mammal); 

$Animalia = new stdClass(); 
$Animalia->description = 'Animalia'; 
$Animalia->children = array($Chordate); 

$catagories = array($Animalia); 

下面是它怎么叫:

$f = new Foo(); 
echo json_encode($f->generateMenu($catagories), JSON_PRETTY_PRINT); 

这里是输出:

[ 
{"title": "Animalia", "expanded": true, "folder": true, "children": 
    [ 
    {"title": "Chordate", "expanded": true, "folder": true, "children": 
     [ 
     {"title": "Mammal", "expanded": true, "folder": true, "children": 
      [ 
      {"title": "Primate", "expanded": true, "folder": true, "children": 
       [ 
       {"title": "Primate", "children": []}, 
       {"title": "Carnivora", "children": []} 
       ] 
      }, 
      {"title": "Carnivora", "expanded": true, "folder": true, "children": 
       [ 
       {"title": "Felidae", "children": []} 
       ] 
      } 
      ] 
     } 
     ] 
    }, 
    {"title": "Arthropoda", "expanded": true, "folder": true, "children": 
     [ 
     {"title": "Insect", "expanded": true, "folder": true, "children": 
      [ 
      {"title": "Diptera", "children": []} 
      ] 
     } 
     ] 
    } 
    ] 
} 
] 
+0

即使如此,如果我隐藏{$ this-> json = array( ); {json的响应是一样的,数据丢失,只显示最后一条记录 – rosuandreimihai 2014-12-02 17:16:53

0

当您遍历JSON时,您正在覆盖数组键。

$array = array(); 
$array['foo'] = 'bar'; 
//$array['foo] = 'bar'; 

$array['foo'] = 'foo'; 
//$array['foo'] = 'foo';