How do you get the value out of two different Json files?



  • There's a code:

        <?php 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://api.travelpayouts.com/v1/city-directions?origin=MOW&token=xxxxxxx&limit=10");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Access-Token: xxxxxx"));
    $response = curl_exec($ch);
    curl_close($ch);
    

    $json=json_decode($response,true);
    ?>
    <?php
    $products=json_decode($response,true);

    foreach ($products as $key => $value) {
    echo "<table class='mytable'><hr>";
    foreach ($value as $k=> $v) {
    echo "<tr><td><br>".$k."</td><td></td></tr>";
    foreach ($v as $g=> $l) {
    $g=str_replace ('price','цена',$g);
    $g=str_replace ('airline','авиакомпания',$g);
    $g=str_replace ('origin','город вылета',$g);
    $g=str_replace ('destination','город прилета',$g);
    $g=str_replace ('transfers','кол-во пересадок',$g);
    $g=str_replace ('flight_number','номер рейса',$g);
    $g=str_replace ('departure_at','дата вылета',$g);
    $g=str_replace ('return_at','дата возвращения',$g);
    $g=str_replace ('expires_at','Срок истечения актуальности цены',$g);

                     echo "&lt;tr&gt;&lt;td&gt;".$g."&lt;/td&gt;&lt;td&gt;".$l."&lt;/td&gt;&lt;/tr&gt;";
            } 
     }
     echo "&lt;/table&gt;";  
    

    }

    ?>

    There are Json files, here they are:
    Country data in json : api.travelpayouts.com/data/countries.jsonUrban data in json format: api.travelpayouts.com/data/cities.jsonAirport data in json format: api.travelpayouts.com/data/airports.jsonAirline data in json format: api.travelpayouts.com/data/airlines.json

    I need Json to change the meanings:
    'airline', 'origin', 'destination'
    That's what the table looks like right now:
    введите сюда описание изображения

    Show me my code as soon as possible.

    API receives this answer:

    {"success": true, "data": [{"show_to_affiliates":false,"trip_class":0,"origin":"PEN","destination":"KUL","depart_date":"2016-04-11","return_date":"2016-04-14","number_of_changes":0,"value":983,"found_at":"2016-01-03T18:33:11+04:00","distance":286,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"UBP","destination":"BKK","depart_date":"2016-07-09","return_date":"2016-07-11","number_of_changes":0,"value":1178,"found_at":"2016-01-03T21:00:12+04:00","distance":502,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"KUL","destination":"KBR","depart_date":"2016-03-23","return_date":"2016-03-31","number_of_changes":0,"value":1295,"found_at":"2016-01-03T22:58:02+04:00","distance":343,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"KUL","destination":"PEN","depart_date":"2016-03-22","return_date":"2016-03-27","number_of_changes":0,"value":1492,"found_at":"2016-01-02T04:15:51+04:00","distance":286,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"BKK","destination":"CNX","depart_date":"2016-05-17","return_date":"2016-05-17","number_of_changes":0,"value":1555,"found_at":"2016-01-03T23:40:36+04:00","distance":583,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"DUS","destination":"ROM","depart_date":"2016-01-28","return_date":"2016-02-02","number_of_changes":0,"value":1580,"found_at":"2016-01-03T02:56:18+04:00","distance":1125,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"NNT","destination":"BKK","depart_date":"2016-06-24","return_date":"2016-06-28","number_of_changes":0,"value":1584,"found_at":"2016-01-03T20:24:11+04:00","distance":565,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"BKK","destination":"UTH","depart_date":"2016-05-28","return_date":"2016-05-30","number_of_changes":0,"value":1590,"found_at":"2016-01-03T21:52:52+04:00","distance":476,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"BLQ","destination":"BUH","depart_date":"2016-01-29","return_date":"2016-02-03","number_of_changes":0,"value":1590,"found_at":"2016-01-03T02:31:12+04:00","distance":1169,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"SUF","destination":"ROM","depart_date":"2016-03-19","return_date":"2016-03-23","number_of_changes":0,"value":1590,"found_at":"2016-01-03T20:13:57+04:00","distance":458,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"DUB","destination":"LON","depart_date":"2016-02-06","return_date":"2016-02-08","number_of_changes":0,"value":1591,"found_at":"2016-01-03T03:16:06+04:00","distance":477,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"DUB","destination":"MAN","depart_date":"2016-01-23","return_date":"2016-02-01","number_of_changes":0,"value":1591,"found_at":"2016-01-03T14:48:54+04:00","distance":265,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"DUB","destination":"NCL","depart_date":"2016-02-03","return_date":"2016-02-09","number_of_changes":0,"value":1591,"found_at":"2016-01-02T01:08:07+04:00","distance":344,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"BKK","destination":"KBV","depart_date":"2016-11-23","return_date":"2016-11-28","number_of_changes":0,"value":1625,"found_at":"2016-01-02T23:07:24+04:00","distance":646,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"LED","destination":"PKV","depart_date":"2016-01-15","return_date":"2016-01-16","number_of_changes":0,"value":1655,"found_at":"2016-01-03T11:12:47+04:00","distance":262,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"KUL","destination":"JHB","depart_date":"2016-02-07","return_date":"2016-02-11","number_of_changes":0,"value":1662,"found_at":"2016-01-03T07:33:14+04:00","distance":276,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"TGG","destination":"KUL","depart_date":"2016-02-20","return_date":"2016-02-27","number_of_changes":0,"value":1662,"found_at":"2016-01-03T12:44:59+04:00","distance":294,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"CGN","destination":"BER","depart_date":"2016-01-20","return_date":"2016-01-22","number_of_changes":0,"value":1677,"found_at":"2016-01-03T22:00:07+04:00","distance":477,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"CGN","destination":"LON","depart_date":"2016-02-10","return_date":"2016-02-11","number_of_changes":0,"value":1678,"found_at":"2016-01-02T04:03:01+04:00","distance":484,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"KUL","destination":"LGK","depart_date":"2016-03-01","return_date":"2016-03-08","number_of_changes":0,"value":1684,"found_at":"2016-01-03T21:55:01+04:00","distance":417,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"HDY","destination":"BKK","depart_date":"2016-11-16","return_date":"2016-11-19","number_of_changes":0,"value":1726,"found_at":"2016-01-03T10:56:00+04:00","distance":754,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"VVO","destination":"KVR","depart_date":"2016-01-10","return_date":"2016-01-17","number_of_changes":0,"value":1735,"found_at":"2016-01-03T05:09:51+04:00","distance":252,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"BRU","destination":"CPH","depart_date":"2016-02-04","return_date":"2016-02-09","number_of_changes":0,"value":1750,"found_at":"2016-01-02T21:00:58+04:00","distance":767,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"BUH","destination":"MIL","depart_date":"2016-02-02","return_date":"2016-02-09","number_of_changes":0,"value":1750,"found_at":"2016-01-03T01:14:21+04:00","distance":1333,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"WAW","destination":"CGN","depart_date":"2016-01-23","return_date":"2016-01-24","number_of_changes":0,"value":1750,"found_at":"2016-01-03T00:56:57+04:00","distance":977,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"MIL","destination":"BUH","depart_date":"2016-02-13","return_date":"2016-02-14","number_of_changes":0,"value":1752,"found_at":"2016-01-03T23:50:37+04:00","distance":1333,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"IST","destination":"AYT","depart_date":"2016-06-06","return_date":"2016-06-29","number_of_changes":0,"value":1793,"found_at":"2016-01-03T12:23:11+04:00","distance":484,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"GDN","destination":"STO","depart_date":"2016-01-22","return_date":"2016-01-24","number_of_changes":0,"value":1802,"found_at":"2016-01-03T23:41:01+04:00","distance":551,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"ATH","destination":"RHO","depart_date":"2016-02-08","return_date":"2016-02-15","number_of_changes":0,"value":1823,"found_at":"2016-01-03T05:29:12+04:00","distance":404,"actual":true},{"show_to_affiliates":false,"trip_class":0,"origin":"BRU","destination":"BUD","depart_date":"2016-02-16","return_date":"2016-03-16","number_of_changes":0,"value":1823,"found_at":"2016-01-03T07:00:43+04:00","distance":1129,"actual":true}]}



  • I don't know who I tried, but it looks like this.

    <?php
    

    $map = function($array, $from, $to){
    $result = [];
    if(!empty($array) && is_array($array)){
    foreach ($array as $element) {
    $key = $element[$from]?:null;
    $value = $element[$to]?:null;
    if($key && $value){
    $result[$key] = $value;
    }
    }
    }
    return $result;
    };

    $airlines = $map(json_decode(file_get_contents('http://api.travelpayouts.com/data/airlines.json'), true), 'iata', 'name');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://api.travelpayouts.com/v1/city-directions?origin=MOW&token=хххххх&limit=10");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Access-Token: ххххххх"));
    $response = curl_exec($ch);
    curl_close($ch);

    $products=json_decode($response,true);

    $replace_pairs=[
    'price' => 'цена',
    'airline'=>'авиакомпания',
    'origin'=>'город вылета',
    'destination'=>'город прилета',
    'transfers'=>'кол-во пересадок',
    'flight_number'=>'номер рейса',
    'departure_at'=>'дата вылета',
    'return_at'=>'дата возвращения',
    'expires_at'=>'Срок истечения актуальности цены'
    ];

    $replace_value = function($key, $val) use ($airlines){
    $response = $val;
    switch($key){
    case 'airline':
    $response = $airlines[$val];
    break;
    }
    return $response;
    }

    ?>

    <?php if(isset($products['data']) && is_array($products['data'])){?>
    <table>
    <?php foreach ($products['data'] as $key => $data) {?>
    <tr>
    <td colspan="2"><?php echo $key?></td>
    </tr>
    <?php if(!empty($data && is_array($data))){?>
    <?php foreach ($data as $field=> $val) {?>
    <tr>
    <td><?php echo strtr($field, $replace_pairs);?></td>
    <td><?php echo $replace_value($field, $val);?></td>
    </tr>
    <?}?>
    <?php }?>
    <?}?>
    </table>
    <?php }?>

    I don't know what files the fields of 'origin', 'destination' are, and there's no desire to be cleared, but I think it's clear.
    At least organize your code a little, so at least you know what's going on. Download/open json files and look at their structure, find at least one of the required meanings, and look at how to get to it, experiment on the code itself, pull out the terations, use debag techniques to understand what you're getting out of there, but it looks like you're gonna need to figure out how they're designed/activated, etc.

    P.S. I suspect that Api himself can build and give what's needed right away, but not sure.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2