2014-12-02 87 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';