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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘