douxian3828 2015-09-04 15:58
浏览 50
已采纳

解码JSON响应的不同元素之间的交叉引用

I'm decoding a JSON response and outputting it in a table. JSON response is separated in three elements (Events, Organizers and Venues) and Events nodes reference nodes from Venues and Organizers elements.

Here is an example of what print_r displays for the JSON response:

Array
(
    [Events] => Array
        (
            [0] => Array
                (
                    [EventTitle] => Concert One                 
                    [Details] => Array
                        (
                            [VenueID] => 100
                            [EventDate] => 2016-01-01
                        )
                )

            [1] => Array
                (
                    [EventTitle] => Concert Two
                    [Details] => Array
                        (
                            [VenueID] => 150
                            [EventDate] => 2016-01-02
                        )
                )

          )

    [Venues] => Array
        (
            [0] => Array
                (
                    [HallID] => 100
                    [VenueName] => Venue A
                )

            [1] => Array
                (
                    [HallID] => 150
                    [VenueName] => Venue B
                )

        )

)

Actual JSON example looks like this:

{
    "Events": [
        {
            "EventTitle": "Concert One",
            "Details": {
                "VenueID": 100,
                "EventDate": "2016-01-01"
            }
        },
        {
            "EventTitle": "Concert Two",
            "Details": {
                "VenueID": 150,
                "EventDate": "2016-01-02"
            }
        }
    ],
    "Venues": [
        {
            "HallID": 100,
            "VenueName": "Venue A"
        },
        {
            "HallID": 150,
            "VenueName": "Venue B"
        }
    ]
}

Here is foreach loop that I use to create the table:

<?php

foreach($results['Events'] as $values)

{
        echo '<tr><td>' . $values['EventTitle'] . '</td>';
        echo '<td>' . $values['Details']['VenueID'] . '</td>';
        echo '<td>' . $values['Details']['EventDate'] . '</td></tr>';
}

?>

It works well and creates a simple table:

Event title | Event venue | Event date
Concert One | 100 | 2016-01-01
Concert Two | 150 | 2016-01-02

What I'm struggling with is how to replace Venue ID (100, 150) with VenueName (Venue A, Venue B), so that the result is:

Event title | Event venue | Event date
Concert One | Venue  A | 2016-01-01
Concert Two | Venue  B | 2016-01-02

Is it possible to achieve this?

  • 写回答

2条回答 默认 最新

  • dqgo99177 2015-09-04 16:15
    关注

    Try this:

    foreach($results['Events'] as $values)
    
    {
            echo '<tr><td>' . $values['EventTitle'] . '</td>';
            echo '<td>' . $results['Venues'][array_search($values['Details']['VenueID'], array_column($results['Venues'], 'HallID'))]['VenueName'] . '</td>';
            echo '<td>' . $values['Details']['EventDate'] . '</td></tr>';
    }
    
    ?>
    

    Everything in one line!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: