2013-11-26 38 views
1

我需要一些正確生成JSON的幫助。我幾乎擁有它,但我需要將每個類別及其相關項目放入其自己的數組中。我有多個類別,每個類別下有很多項目。我的JSON看起來不錯,我只需要將這些類別作爲一個數組以及它的項目。任何幫助是極大的讚賞!PHP JSON添加陣列

這裏是我的代碼:

$channel ['items']['post']['categories'][$category_name]['details'][] = array(
    'title' => $title, 
    'price' => $price, 
    'date_time' => $date_time, 
    'description' => $description, 

    ); 
} 
$channels = array($channel); 
$json = json_encode($channel); 
header('Content-type: application/json'); 
echo $json; 

我的JSON看起來像這樣目前:

{ 
"items": { /// THIS IS MEANT TO ORGANIZE UNDER ITEMS 
    "post": { ///THIS IS PROBABLY UNNECESSARY, I JUST HAVEN'T REMOVED IT. 
     "categories": { 
      "Baby": { ///I HAVE MANY MANY CATEGORIES 
       "details": [ ///I HAVE MANY MANY ITEMS UNDER EACH CATEGORY 
        { 
         "title": "trying with category id again", 
         "price": "3344.55", 
         "date_time": "2013-11-11 17:33:49", 
         "description": "Descriptor sb ", 
         "category_id": "3", 
        }, 

       ] 

但我希望它看起來像這樣:

{ 
"items": { 
     "categories": [{ /// NEED THE BRACKET HERE 
      "Baby": { 
       "details": [ 
        { 
         "title": "trying with category id again", 
         "price": "3344.55", 
         "date_time": "2013-11-11 17:33:49", 
         "description": "Descriptor sb ", 
        }, 
        { 
         "title": "what the", 
         "price": "44444.66", 
         "date_time": "2013-11-18 20:15:58", 
         "description": "Blah blah", 
        }, 
       ] 
      }, 
      "Baby Stuff": { 
       "details": [ 
        { 
         "title": "putting in title", 
         "price": "3000.99", 
         "date_time": "2013-11-11 17:42:15", 
         "description": "Blah blah blah", 
        }, 
        { 
         "title": "adding another listing", 
         "price": "400000.99", 
         "date_time": "2013-11-17 22:37:02", 
         "description": "Blah blah blah", 
        }, 


       ] 
      }, 
      "More Baby Stuff": { 
       "details": [ 
        { 
         "title": "does this work", 
         "price": "4000.77", 
         "date_time": "2013-11-18 19:59:49", 
         "description": "Description ", 
        }, 
        { 
+0

當你希望做的是容易因與建議去實現什麼回答@maček下面。您的數據結構化方式似乎仍然很奇怪。例如,在根級別你有一個名爲「items」(複數)的屬性,在我看來,它會有一個數組作爲值,而不是一個名爲'post'的嵌套對象。同樣的,你可以在'categories'中有一個arrary,但爲什麼你會比名稱爲'Baby'的元素中的某個屬性更具有屬性名稱,比如'category name =>'baby''。這看起來像是一個奇怪的結構訪問 –

+0

你是正確的發佈。它可能是無意義的,我會刪除它。但現在,它是這樣的,因爲我有很多類別,每個類別都有很多項目。我需要按類別和類別下的所有項目以關係方式構建JSON。 – mreynol

+0

爲了繼續我的評論......我認爲如果你用數組和對象來思考,不僅僅是關聯數組,而且關聯數組通過JSON被轉換爲對象。什麼應該是一個到另一個的關係?問你自己在你的結構中的每個節點,下一個葉應該是一個數組還是一個對象。例如,從頂部開始,'items'包含它下面的多個對象(在這種情況下數組可能適用),還是它確實包含單個項目(在這種情況下,對象將是合適的)。請在您的數據結構中添加註釋。 –

回答

2

添加另一個[]['categories']並且應該這樣做:

$channel ['items']['post']['categories'][][$category_name]['details'][] = array(// ... 

$channel ['items']['post']['categories'][]['Baby']['details'][] = array(
    'title' => 'trying with category id again', 
    'price' => 3344.55, 
    'date_time' => '2013-11-11 17:33:49', 
    'description' => 'Descriptor sb', 

    ); 

$channels = array($channel); 
$json = json_encode($channel); 
echo $json; 

輸出基於評論

{ 
    "items": { 
     "post": { 
      "categories": [ 
       { 
        "Baby": { 
         "details": [ 
          { 
           "title": "trying with category id again", 
           "price": 3344.55, 
           "date_time": "2013-11-11 17:33:49", 
           "description": "Descriptor sb" 
          } 
         ] 
        } 
       } 
      ] 
     } 
    } 
} 

See it working here on ideone

+0

我可能沒有正確地問我的問題。 :)。我做了這個改變,但它重複每個類別名稱爲每個項目的細節與該類別的名稱。我需要更多的關係。我有14個類別...每個類別下都有幾個項目詳細信息。我需要該類別僅顯示其中的每個項目一次。那有意義嗎? – mreynol

1

和編輯到OP,也許你應該尋找這樣的數據結構:

{ 
    "categories": 
    [ 
     { 
     "category_name": "Baby", 
     ... [Other category metadata], 
     "category_items": 
      [ 
      { 
       "item_name": "Item Name", 
       ... [Other item information] 
      }, 
      { [next item] }, 
      ... 
      ] 
     }, 
     { [ next category ] }, 
     ... 
    ] 
} 

這意味着人們可以建立類似的方式該對象以下(假設比如你是從數據庫查詢構建此):

$channel = new stdClass(); 
$channel->categories = array(); 

LOOP for categories as $category 
    $category = new stdClass(); 
    $category->category_name = 'some value'; 
    $category->some_other_category_metadata = 'some other value'; 
    $category->category_items = array(); 
    INNER LOOP on items in category as $item 
     $item = new stdClass(); 
     $item->item_name = 'item name'; 
     $item->some_other_item_data = 'data'; 
     $category->category_items[] = $item; 
    END INNER LOOP 
    $channel->categories[] = $category; 
END OUTER LOOP 

json_encode($channel); 
+0

我用更多的JSON更新了我的帖子,以更好地說明我想要得到什麼。我並不完全關注你粘貼的代碼。正如你所看到的,我有多個類別。每個類別中包含多個項目。我相信我只需要在他們自己的陣列中包含每個類別和相關項目。 – mreynol