簡體   English   中英

使用PHP構建嵌套JSON時遇到問題

[英]Having trouble building nested JSON with PHP

我在使這些數組正確嵌套方面遇到麻煩。 我正在從SQL數據庫中獲取所有行,並為我的原始應用程序構建一個數組以JSON輸出。 一個類別很容易,但是客戶現在想要子類別,這就是我碰壁的時候。 我試圖將這些子類別嵌套到類別中。 我可以執行任何操作,但是無法使他們一起工作。 以下是我的PHP代碼:

while ($row = $query->fetch_assoc()) {
    $categories[$row['category']][] = array(
        $row['subcategory'] => $subcategories[$row['subcategory']][] = array(
            'id'                 => $row['id'],
            'item_name'          => $row['item_name'],
            'description'        => $row['description'],
            'price'              => $row['price'],
        ),
    );
}

foreach ($categories as $key => $value) {
    $category_data[] = array(
        'category'   => $key,
        'category_list' => $value,
    );
}

foreach ($subcategories as $key => $value) {
    $subcategory_data[] = array(
        'subcategory'   => $key,
        'subcategory_list' => $value,
    );
}

當我json_encode($ category_data)時,我得到以下JSON:

[
{
    "category": "Beer",
    "category_list": [
        {
            "Draft Beer": {
                "id": "1",
                "item_name": "Yuengling",
                "description": "Lager. Pottstown, Pa. An American classic."
            }
        },
        {
            "Draft Beer": {
                "id": "6",
                "item_name": "Bud Light",
                "description": "American Light Lager"
            }
        },
        {
            "Domestic Bottles": {
                "id": "9",
                "item_name": "Stone IPA",
                "description": "India Pale Ale.<em> Escondido, Colo."
            }
        }
    ]
},
{
    "category": "Wine",
    "category_list": [...]
}
]

可能可行,但我希望將類似的鍵(例如:生啤酒)連接到同一數組中。 當我如下所示json_encode($ subcategory_data)時,它確實做到了,但是它沒有分成幾類,只是子類。

[
{
    "subcategory": "Draft Beer",
    "subcategory_list": [
        {
            "id": "1",
            "item_name": "Yuengling",
            "description": "Lager. Pottstown, Pa."
        },
        {
            "id": "6",
            "item_name": "Bud Light",
            "description": "American Light Lager. Milwaukee, Wisc."
        }
    ]
},
{
    "subcategory": "Red Wine",
    "subcategory_list": [
        {
            "id": "17",
            "item_name": "Kendall-Jackson",
            "description": "Cabernet Sauvignon, 2010."
        },
        {
            "id": "18",
            "item_name": "Sanford",
            "description": "Pinot Noir, 2011. Lompoc, Calif"
        }
    ]
},
{
    "subcategory": "Domestic Bottles",
    "subcategory_list": [
        {
            "id": "9",
            "item_name": "Stone IPA",
            "description": "India Pale Ale. Escondido, Colo."
        },
        {
            "id": "10",
            "item_name": "Blue Moon",
            "description": "Belgian-style Wheat Ale."
        }
    ]
}
]

所以我的問題是為什么它會合並到第二組數據中而不合並到第一組數據中? 如何將subcategory_data放入category_data。 任何幫助ID的真正贊賞。 以下是我要尋找的示例:

{
    "category_name":"Beer",
    "category_list" : [
        {
            "subcategory_name": "Draft Beer",
            "subcategory_list": [
            {
                "id": "1",
                "item_name": "Yuengling",
                "description": "Lager. Pottstown, Pa."
            },
            {
                "id": "6",
                "item_name": "Bud Light",
                "description": "American Light Lager. Milwaukee, Wisc."
            }
        }
    ]   
},
{
    "category_name":"Wine",
    "category_list" : [
        {
            "subcategory_name": "Red Wine",
            "subcategory_list": [...]
        }
    ]
}

感謝您的關注,我對PHP還是相當陌生,並且依靠我的JavaScript技能。

首先,嘗試var_dump($categories) 您會看到您正在構建一個相當奇怪的數據結構(您擁有1元素數組的數組...)。 以下代碼將構建一個更簡單的結構:

$categories[$row['category']][$row['subcategory']][] = array(
  'id'                 => $row['id'],
  'item_name'          => $row['item_name'],
  'description'        => $row['description'],
  'price'              => $row['price'],
  );

這將序列化為可以接受的JSON,如下所示:

{
  "Beer": {
    "Draft Beer": [
      {
        "id": "1",
        "item_name": "Yuengling",
        "description": "Lager. Pottstown, Pa."
      },
      {
        "id": "6",
        "item_name": "Bud Light",
        "description": "American Light Lager. Milwaukee, Wisc."
      }
    ],
    ...

另一個轉換將以您要求的格式獲取數據:

foreach ($categories as $category_name => $subcategories) {
  $subcategory_data = array();
  foreach ($subcategories as $subcategory_name => $items) {
    $subcategory_data[] = array(
      'subcategory_name' => $subcategory_name,
      'subcategory_list' => $items
      );
  }
  $category_data[] = array(
    'category_name' => $category_name,
    'category_items' => $subcategory_data
    );                 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM