【问题标题】:PHP loop and rebuild array grouped by value (topic) output JSONPHP循环和重建数组按值(主题)输出JSON分组
【发布时间】:2017-12-20 17:28:22
【问题描述】:

我从 MYSQL 得到这个结果; content_groups 已经从按 TopicId 排序的 MYSQL 返回。

Array
(
    [0] => Array
        (
            [ContentGroupId] => 1
            [TopicId] => 1
            [TopicName] => Meditations
            [ContentGroupName] => Guided Meditations 1
        )

    [1] => Array
        (
            [ContentGroupId] => 2
            [TopicId] => 1
            [TopicName] => Meditations
            [ContentGroupName] => Guided Meditations 2
        )

    [2] => Array
        (
            [ContentGroupId] => 3
            [TopicId] => 1
            [TopicName] => Meditations
            [ContentGroupName] => Guided Meditations 3
        )

    [3] => Array
        (
            [ContentGroupId] => 4
            [TopicId] => 2
            [TopicName] => Hypnosis
            [ContentGroupName] => Hypnosis Programs 1
        )

    [4] => Array
        (
            [ContentGroupId] => 5
            [TopicId] => 2
            [TopicName] => Hypnosis
            [ContentGroupName] => Hypnosis Programs 2
        )

    [5] => Array
        (
            [ContentGroupId] => 6
            [TopicId] => 3
            [TopicName] => Mindfulness
            [ContentGroupName] => Mindfulness Guides 1
        )

    [6] => Array
        (
            [ContentGroupId] => 7
            [TopicId] => 3
            [TopicName] => Mindfulness
            [ContentGroupName] => Mindfulness Guides 2
        )

    [7] => Array
        (
            [ContentGroupId] => 8
            [TopicId] => 3
            [TopicName] => Mindfulness
            [ContentGroupName] => Mindfulness Guides 3
        )

我正在尝试遍历此内容,以便按主题和 JSON 编码进行分组,如下所示:

topics: [
    {
      TopicId: 1,
      TopicName: Mediations,
      content_groups: [
        {
           ContentGroupId: 1,
           TopicId: 1,
           TopicName: "Mediations",
           ContentGroupName: "Guided Meditations 1"
        },
        {
           ContentGroupId: 2,
           TopicId: 1,
           TopicName: "Mediations",
           ContentGroupName: "Guided Meditations 2"
        },
        {
           ContentGroupId: 3,
           TopicId: 1,
           TopicName: "Mediations",
           ContentGroupName: "Guided Meditations 3"
        }
      ]
    },
    {
      TopicId: 2,
      TopicName: Hypnosis,
      content_groups: [
        {
           ContentGroupId: 4,
           TopicId: 2,
           TopicName: "Hypnosis",
           ContentGroupName: "Hypnosis Programs 1"
        },
        {
           ContentGroupId: 5,
           TopicId: 2,
           TopicName: "Hypnosis",
           ContentGroupName: "Hypnosis Programs 2"
        }
      ]
    },
    {
      TopicId: 3,
      TopicName: Mindfulness,
      content_groups: [
        {
           ContentGroupId: 6,
           TopicId: 3,
           TopicName: "Hypnosis",
           ContentGroupName: "Mindfulness Guides 1"
        },
        {
           ContentGroupId: 7,
           TopicId: 3,
           TopicName: "Hypnosis",
           ContentGroupName: "Mindfulness Guides 2"
        },
        {
           ContentGroupId: 8,
           TopicId: 3,
           TopicName: "Hypnosis",
           ContentGroupName: "Mindfulness Guides 3"
        }
      ]
    }
 ]

我最初只是在循环每个主题以获取相应的内容组时进行了单独的 MYSQL 调用,但是随着主题的增长,这似乎效率低下,因为我可以更轻松地进行一次 SQL 调用以获取所有需要的内容组然后重建要按相应主题分组的数组,但我正在努力弄清楚如何输出它以获得所需的最终结果。

【问题讨论】:

    标签: php mysql arrays loops


    【解决方案1】:

    在遍历 MySQL 查询结果创建数组时,然后对该数组使用 json_encode

    $query= mysqli_query("SELECT ...");
    $rows = array();
    while($result = mysqli_fetch_assoc($query)) {
    $rows[] = $result;
    }
    print json_encode($rows);
    

    【讨论】:

    • 这不会生成 OP 要求的重组结果。
    【解决方案2】:

    使用TopicId 作为临时关联键,isset() 可用于有效地确定您是在处理新组的数据还是适当的组已经存在。

    代码:(Demo)

    $array = [
        ['ContentGroupId' => 1, 'TopicId' = >1, 'TopicName' => 'Meditations', 'ContentGroupName' => 'Guided Meditations 1'],
        ['ContentGroupId' => 2, 'TopicId' => 1, 'TopicName' => 'Meditations', 'ContentGroupName' => 'Guided Meditations 2'],
        ['ContentGroupId' => 3, 'TopicId' = >1, 'TopicName' => 'Meditations', 'ContentGroupName' => 'Guided Meditations 3'],
        ['ContentGroupId' => 4, 'TopicId' => 2, 'TopicName' => 'Hypnosis', 'ContentGroupName' => 'Hypnosis Programs 1'],
        ['ContentGroupId' => 5, 'TopicId' => 2, 'TopicName' => 'Hypnosis', 'ContentGroupName' => 'Hypnosis Programs 2'],
        ['ContentGroupId' => 6, 'TopicId' => 3, 'TopicName' => 'Mindfulness', 'ContentGroupName' => 'Mindfulness Guides 1'],
        ['ContentGroupId' => 7, 'TopicId' => 3, 'TopicName' => 'Mindfulness', 'ContentGroupName' => 'Mindfulness Guides 2'],
        ['ContentGroupId' => 8, 'TopicId' => 3, 'TopicName' => 'Mindfulness', 'ContentGroupName' => 'Mindfulness Guides 3']
    ];
    foreach ($array as $a) {
        if (!isset($grouped[$a['TopicId']])) {
            $grouped[$a['TopicId']] = array_slice($a, 1, 2);  // write TopicID & TopicName just once to group
        }
        $grouped[$a['TopicId']]['content_groups'][] = $a;   // write all elements to content_groups
    }
    $grouped=['topics' => array_values($grouped)];  // re-index temp keys and nest inside topics element
    var_export(json_encode($grouped));
    

    输出:

    '{"topics":[{"TopicId":1,"TopicName":"Meditations","content_groups":[{"ContentGroupId":1,"TopicId":1,"TopicName":"Meditations","ContentGroupName":"Guided Meditations 1"},{"ContentGroupId":2,"TopicId":1,"TopicName":"Meditations","ContentGroupName":"Guided Meditations 2"},{"ContentGroupId":3,"TopicId":1,"TopicName":"Meditations","ContentGroupName":"Guided Meditations 3"}]},{"TopicId":2,"TopicName":"Hypnosis","content_groups":[{"ContentGroupId":4,"TopicId":2,"TopicName":"Hypnosis","ContentGroupName":"Hypnosis Programs 1"},{"ContentGroupId":5,"TopicId":2,"TopicName":"Hypnosis","ContentGroupName":"Hypnosis Programs 2"}]},{"TopicId":3,"TopicName":"Mindfulness","content_groups":[{"ContentGroupId":6,"TopicId":3,"TopicName":"Mindfulness","ContentGroupName":"Mindfulness Guides 1"},{"ContentGroupId":7,"TopicId":3,"TopicName":"Mindfulness","ContentGroupName":"Mindfulness Guides 2"},{"ContentGroupId":8,"TopicId":3,"TopicName":"Mindfulness","ContentGroupName":"Mindfulness Guides 3"}]}]}'
    

    【讨论】:

    • 谢谢@mickmackusa 我正在敲我的头......这似乎是一种非常干净的方法;比我尝试使用的代码要少得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 2015-08-07
    相关资源
    最近更新 更多