Как вытащить значение из 2-х разных Json файлов?
Есть код:
<?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 "<tr><td>".$g."</td><td>".$l."</td></tr>"; } } echo "</table>"; } ?> Есть Json файлы, вот они: Данные о странах в json : api.travelpayouts.com/data/countries.json Данные о городах в json формате: api.travelpayouts.com/data/cities.json Данные об аэропортах в json формате: api.travelpayouts.com/data/airports.json Данные об авиакомпаниях в json формате: api.travelpayouts.com/data/airlines.json Мне нужно, чтобы из Json заменялись значения: 'airline', 'origin', 'destination', Вот как выглядит таблица сейчас: ![]() Вот ответ API: Код:
{"success": true, "data": [{"show_to_affiliates":false,"trip_class":0,"origin":"PEN","destination":"KUL","depart_date":"2016-04-11", |
Строка 10 зачем, дважды под разными переменными одно и тоже?
Из показанного ответа: Код:
Array Где остальные ключи описанные и все ли они нужны? |
Цитата:
Вот так вылядит ответ API: {"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}, Вот так ответ json: {"code":"ABW","name":"Abau","coordinates":{"lon":148.7,"lat":-10.183333} "code" - это название авиакомпании "name" - это город. Он может быть городом прилета или вылета. |
Цитата:
Цитата:
А то получается данные с разной структурой, а вы гоняете все одним и тем же кодом. |
Цитата:
Цитата:
api.travelpayouts.com/data/cities.json |
вариант перевода на клиенте
sochi-russia,
на всякий случай ... <!DOCTYPE HTML> <html> <head> <title>Untitled</title> <meta charset="utf-8"> <style type="text/css"> td { outline: solid 1px #000000; background-image: -webkit-gradient(linear, top, bottom, color-stop(0, #E6E6E6), color-stop(1, #A9A9A9)); background-image: -o-linear-gradient(top, #E6E6E6, #A9A9A9); background-image: -moz-linear-gradient(top, #E6E6E6, #A9A9A9); background-image: -webkit-linear-gradient(top, #E6E6E6, #A9A9A9); background-image: linear-gradient(to bottom, #E6E6E6, #A9A9A9) } </style> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script> $(function(){ var cities = { "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": "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": "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": "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": "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": "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 }] }; var rep = { 'value':'цена', 'airline':'авиакомпания', 'origin':'город вылета', 'destination':'город прилета', 'number_of_changes':'кол-во пересадок', 'flight_number':'номер рейса', 'depart_date':'дата вылета', 'return_date':'дата возвращения', 'distance': 'расстояние', 'found_at' : 'Срок истечения актуальности цены' }; var mytable = $("<table/>"); $.each(cities.data, function(i, myData) { var item = Object.keys(myData).filter(function(el) { return rep[el] }); $(mytable).append($.map(item, function(key) { return $("<tr/>").append( $("<td/>", {text: rep[key] || key}), $("<td/>", {text: myData[key]})) })) }); $("body").append(mytable); }); </script> </head> <body> </body> </html> |
Не цитируйте все, что я пишу, иначе пост превратится в длинную портянку мусора.
Цитата:
Я вот смотрю на ваш код foreach($products as $k=>$v) ... foreach ($value as $k=> $v) { echo "<tr><td><br>".$k."</td><td></td></tr>"; foreach ($v as $g=> $l) { .... и представлен ответ сервера, а его первичные ключи в echo "<tr><td><br>".$k."</td><td></td></tr>" выдали бы 1 и Array. Более того, значение первого ключа success это булево значение, нельзя применить к нему foreach, это уже ошибка. Так что описывайте и показывайте, или вы хотите чтобы еще кто-то эти данные получил, догадался какие из них вам нужны и как их вывести? |
Часовой пояс GMT +3, время: 16:23. |