【问题标题】:Having trouble building nested JSON with PHP使用 PHP 构建嵌套 JSON 时遇到问题
【发布时间】:2014-08-31 00:09:09
【问题描述】:

我无法让这些数组正确嵌套。我正在从 SQL 数据库中获取所有行,并为我的 crud 应用程序构建一个以 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 技能。

【问题讨论】:

    标签: php arrays json merge


    【解决方案1】:

    首先,试试var_dump($categories)。你会看到你正在构建一个相当奇怪的数据结构(你有一个元素数组的数组......)。以下代码将构建一个更简单的结构:

    $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
        );                 
    }
    

    【讨论】:

    • 哇,这就像一个魅力!我知道这个结构很疯狂,但它必须是因为我不知道新类别的名称,以及未来的子类别。我有一种预感,你可以在那里做 2 次 foreach,但它失控了,我真的不知道该往哪个方向走。再次感谢!
    猜你喜欢
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多