@sochi-russia

Почему не собираются данные c API?

Есть код
<?php 
$json= curl_init(); 
$json=json_decode($response,true);
$json=end($json['data']);


foreach($json as $val) { ?>
<?
$mapping = json_decode(file_get_contents('http://site.com/airlines/airlines.json'));
$name = '';
$code = $val['destination'];
foreach ($mapping as $item) {
    if ($item->iata == $code) {
        $name = $item->name;
        $departure = $item->departure_at;
        break;
    }
}
?>
<div style="background:#ffffff; color:#008BE0!important;font-weight:bold;">Вылет: <?=$val['departure_at']?> Цена: <?=$val['price']?> Авиакомпания: <?=$name?> Номер рейса: <?=$val['flight_number']?></div>
.....

<?php } ?>




У API Ответ:

{"success":true,"data":{"AAQ":{"origin":"REN","destination":"AAQ","price":15450,"transfers":1,"airline":"SU","flight_number":5831,"departure_at":"2016-03-17T19:30:00Z","return_at":"2016-03-28T11:30:00Z","expires_at":"2016-02-09T09:28:27Z"},"AER":{"origin":"REN","destination":"AER","price":15450,"transfers":1,"airline":"SU","flight_number":5833,"departure_at":"2016-03-20T09:00:00Z","return_at":"2016-03-23T06:50:00Z","expires_at":"2016-02-09T09:18:50Z"},"DYU":{"origin":"REN","destination":"DYU","price":25927,"transfers":0,"airline":"SZ","flight_number":212,"departure_at":"2016-02-10T14:10:00Z","return_at":"2016-02-24T09:30:00Z","expires_at":"2016-02-06T16:54:52Z"},"KRR":{"origin":"REN","destination":"KRR","price":15450,"transfers":1,"airline":"SU","flight_number":5831,"departure_at":"2016-03-10T19:30:00Z","return_at":"2016-04-17T10:25:00Z","expires_at":"2016-02-09T04:09:34Z"},"LBD":{"origin":"REN","destination":"LBD","price":31345,"transfers":2,"airline":"U6","flight_number":2625,"departure_at":"2016-07-12T17:00:00Z","return_at":"2016-07-13T21:40:00Z","expires_at":"2016-02-06T18:21:49Z"},"LED":{"origin":"REN","destination":"LED","price":9599,"transfers":1,"airline":"SU","flight_number":1245,"departure_at":"2016-02-27T05:55:00Z","return_at":"2016-04-24T06:25:00Z","expires_at":"2016-02-07T18:45:53Z"},"MOW":{"origin":"REN","destination":"MOW","price":8250,"transfers":0,"airline":"FV","flight_number":6754,"departure_at":"2016-06-09T07:40:00Z","return_at":"2016-06-09T18:45:00Z","expires_at":"2016-02-06T19:17:10Z"},"NUX":{"origin":"REN","destination":"NUX","price":20000,"transfers":1,"airline":"SU","flight_number":1249,"departure_at":"2016-03-14T21:00:00Z","return_at":"2016-04-15T16:05:00Z","expires_at":"2016-02-07T03:38:33Z"},"SIP":{"origin":"REN","destination":"SIP","price":15450,"transfers":1,"airline":"SU","flight_number":5833,"departure_at":"2016-02-12T09:00:00Z","return_at":"2016-02-16T15:15:00Z","expires_at":"2016-02-07T09:08:39Z"},"SVX":{"origin":"REN","destination":"SVX","price":18600,"transfers":2,"airline":"R2","flight_number":5833,"departure_at":"2016-03-17T09:00:00Z","return_at":"2016-03-18T06:15:00Z","expires_at":"2016-02-07T06:01:23Z"}},"error":null,"currency":"rub"}




Код рабочий, я его использовал для другого АПИ, но здесь вот с этой частью не могу разобраться
foreach ($mapping as $item) {
        if ($item->iata == $code) {
            $name = $item->name;
            $departure = $item->departure_at;
            break;
        }


Мне нужно взять данные destination, Помогите пожалуйста.
  • Вопрос задан
  • 164 просмотра
Пригласить эксперта
Ответы на вопрос 1
iCoderXXI
@iCoderXXI
React.JS/FrontEnd engineer
попробуйте вот так

foreach ($mapping->data as $key->$item) {

Кстати в представленном JSON нет поля name... может быть надо использовать $key или $item->destination?
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы