如何使用php和mysql在JSON中打印类别树

时间:2014-01-30 15:13:29

标签: php mysql json tree categories

我在使用JSON打印类别树的功能时遇到了一些问题。

这些是我的表格:

tb_category

ID_CATEG                    CATEGORY
   1                       Category 1
   2                       Category 2
   3                       Category 3

tb_subcategory

ID_SUBCATEG                   SUBCATEGORY              ID_CATEG
    1                         Subcategory 1               1
    2                         Subcategory 2               2
    3                         Subcategory 3               2
    4                         Subcategory 4               2
    5                         Subcategory 5               3
    6                         Subcategory 6               3

如何使用 PHP 打印JSON,如下所示:

{
    [
        "category": "Category 1",
        "subcategories": {
            "Subcategory 1"
        }
    ],
    [
        "category": "Category 2",
        "subcategories": {
            "Subcategory 2",
            "Subcategory 3",
            "Subcategory 4"
        }
    ],
    [
        "category": "Category 3",
        "subcategories": {
            "Subcategory 5",
            "Subcategory 6"
        }
    ],

}

或类似的东西。

0 个答案:

没有答案