S
S
sochi-russia2015-12-24 23:17:34
JavaScript
sochi-russia, 2015-12-24 23:17:34

Api and json how to translate airline code to normal name?

Yes, it is difficult for me, but I do not give up.
I have a code, I've been working on it for a week

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.travelpayouts.com/v1/prices/cheap?origin=MOW&destination=HKT&token=321d6a221f8926b5ec41ae89a3b2ae7b");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"X-Access-Token: 321d6a221f8926b5ec41ae89a3b2ae7b"
));
$response = curl_exec($ch);
curl_close($ch);
$json=json_decode($response,true);?>
<?php
$json= curl_init();
$json=json_decode($response,true);
$json=end($json['data']);
foreach($json as $val) { ?>
<div style="background:#ffffff; color:#008BE0!important;font-weight:bold;">Цена: <?=$val['price']?> Авиакомпания: <?=$val['airline']?> Номер рейса: <?=$val['flight_number']?></div>
.....

<?php } ?>

The API itself gives the answer:
{"success": true, "data": {"HKT":{"0":{"price":32539,"airline":"R2","flight_number":559,"departure_at":"2015-12 -31T23:00:00Z","return_at":"2016-01-03T14:10:00Z","expires_at":"2015-12-27T19:46:58Z"},"1":{"price": 31521,"airline":"CZ","flight_number":6002,"departure_at":"2016-03-17T21:40:00Z","return_at":"2016-03-29T02:25:00Z","expires_at ":"2015-12-25T13:43:26Z"},"2":{"price":32057,"airline":"CZ","flight_number":356,"departure_at":"2016-03-18T21 :40:00Z","return_at":"2016-03-26T01:50:00Z","expires_at":"2015-12-25T14:09:29Z"},"3":{"price":48458,"airline":"KC","flight_number":894,"departure_at":"2016-05-02T10:00: 00Z","return_at":"2016-05-15T21:10:00Z","expires_at":"2015-12-26T08:38:56Z"}}}}

Convert to human form
Price: 32539 Airline: R2 Flight number: 559
.....
Price: 31521 Airline: CZ Flight number: 6002
.....
Price: 32057 Airline: CZ Flight number: 356
.....
Price: 48458 Airline: KC Flight number: 894

<?php
$json= curl_init();
$json=json_decode($response,true);
$json=end($json['data']);
foreach($json as $val) { ?>

<div style="background:#ffffff; color:#008BE0!important;font-weight:bold;">Цена: <?=$val['price']?> Авиакомпания: <?=$val['airline']?> Номер рейса: <?=$val['flight_number']?></div>
.....

<?php } ?>

everything works fine, but the airline is written in the form of a two-letter code, here in the example it is "R2"
I asked the administration of travelpayouts.com how to translate the normal form. The administration replied that I can use the json file for my own purposes. Question is this file
api.travelpayouts.com/data/airlines.json

How can I use it (translate the airline code into normal verbal form.)
Thanks in advance. Sincerely

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Polishchuk, 2015-12-24
@sochi-russia

$mapping = json_decode(file_get_contents('test.json'));
$code = 'R2';
$name = '';
foreach ($mapping as $item) {
    if ($item->iata == $code) {
        $name = $item->name;
        break;
    }
}
echo $name;

I haven't tested it, but it should work
UPD:
I'll combine your code with mine for understanding
foreach($json as $val) { ?>
<?
$mapping = json_decode(file_get_contents('test.json'));
$name = '';
$code = $val['airline']
foreach ($mapping as $item) {
    if ($item->iata == $code) {
        $name = $item->name;
        break;
    }
}
?>
<div style="background:#ffffff; color:#008BE0!important;font-weight:bold;">Цена: <?=$val['price']?> Авиакомпания: <?=$name?> Номер рейса: <?=$val['flight_number']?></div>
.....

<?php } ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question