douli6605 2013-08-05 18:56 采纳率: 100%
浏览 21
已采纳

回声部分json在php中返回

my returned json looks like this http://pastebin.com/Nbr161s3

I want to echo

body->airTicketListResponse->routings->mainAirlineName
body->airTicketListResponse->routings->adultBasePrice
body->airTicketListResponse->routings->trips->segments->departureAirportCode
body->airTicketListResponse->routings->trips->segments->departureTime //only the time here
body->airTicketListResponse->routings->trips->segments->duration

for each routings.

How do I do this? Here is what I have but I am lost and I know it is way off.

$result = data returned here http://pastebin.com/Nbr161s3
$airTicketListResponse = $result->body->airTicketListResponse;
$routings = $result->body->airTicketListResponse->routings;
$trips = $result->body->airTicketListResponse->routings->trips;
$segments = $result->body->airTicketListResponse->routings->trips->segments;

foreach($airTicketListResponse as $item){
    $i=0; 
    $i<count($routings); 

    echo '<span style="font-weight:bold;">Airline - '.$item->routings[i]->mainAirlineName.' Price - '.$item->routings[i]->adultBasePrice.'</span><br />'.$item->routings[i]->trips[i]->segments[i]->departureAirportCode.' '.$item->routings[i]->trips[i]->segments[i]->departureTime.'<br /><br />';
    $i++;
    }

Please help if you can.

  • 写回答

2条回答 默认 最新

  • duanjue6584 2013-08-05 19:43
    关注

    Before working with JSON you should be familiar with working with arrays and objects since JSON is nothing more than that.

    It seems you already know these two concepts

    1. To access an object property in PHP you use obj->property
    2. To access a value of an array you specify the index inside brackets array[0]

    With JSON you just have to keep in mind that some of your object properties will be arrays.

    Now, since your data comes in a multi-level three-like structure you should also be familiar with traversing arrays, PHP offers an implementation of a foreach loop that is ideal for traversing dynamically generated arrays

    using foreach($array as $index => $var) the $index and $var variables are automatically set to the index and value of each element in the array as they are being traversed, so you don't manually need to keep track of the index (i.e. $i)

    Now let's start going through your data:

    First we find your routings array

    $result = json_decode($data);
    $airTicketListResponse = $result->body->airTicketListResponse;
    $routings = $airTicketListResponse->routings;
    

    Now we use foreach to loop through every routing and print the needed properties

    foreach($routings as $routing){ //$routing will hold the object value in each loop
        echo 'Airline '.$routing->mainAirlineName.'<br>';
        echo 'Adult Base Price '.$routing->adultBasePrice.'<br>';
    }
    

    Getting single properties like the above is pretty straight forward, but for the information of the segments we would first need a nested foreach since we have multiple trips for each routing and then a second nested foreach since each trip has multiple segments

    foreach($routings as $routing){ //$routing will hold the object value in each loop
        echo 'Airline '.$routing->mainAirlineName.'<br>';
        echo 'Adult Base Price '.$routing->adultBasePrice.'<br>';
        foreach($routing->trips as $trip){
            foreach($trip->segments as $index => $segment){
                echo 'Segment '.$index.':<br>'
                echo 'Depart From '.$segment->departureAirportCode.'<br>';
                echo 'Departure Time '.$segment->departureTime.'<br>';
                echo 'Duration '.$segment->duration.'<br>';
            }
        }
    }
    

    And that would be it. I hope my explanation was clear and you got the idea of how to traverse JSON objects

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改