There is a code

<?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://aviaciya.com/json/airlines.json'), true), 'iata', 'name'); $cit=json_decode(file_get_contents("http://aviaciya.com/json/cities.json")); $cities=array(); foreach($cit as $val) { if(property_exists($val->name_translations,"ru")) $cities[$val->code]=$val->name_translations->ru;} $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://api.travelpayouts.com/v1/prices/cheap?origin=MOW&destination=-&token=*******"); 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_value = function($key, $val) use ($cities, $airlines){ $response = $val; switch($key){case 'destination': $response = $cities[$val];break; case 'origin': $response = $cities[$val];break; case 'airline': $response = $airlines[$val]; break; } return $response; } ?> <table class="features-table"> <thead style="background: rgba(2, 69, 141, 0.46) none repeat scroll 0% 0%; color: rgb(255, 255, 255);"> <tr> <th class="views-field views-field-field-logo-fid"> Пункт отправления </th> <th class="views-field views-field-tid"> Пункт назначения </th> <th class="views-field views-field-tid-1"> Авиакомпания </th> <th class="views-field views-field-tid-1"> Цена, р. </th> <th class="views-field views-field-field-cenaot-value"> Дата вылета </th> <th class="views-field views-field-field-prodsgapo-value">Обратная дата </th> <th class="views-field views-field-field-perelets-value"></th> </tr> </thead> <?php if(isset($products['data']) && is_array($products['data'])) { foreach ($products['data'] as $key => $data) { foreach ($data as $destination => $row) { if (preg_match('/[AZ]{3}/i', $key)) { ?> <tr><td><?php echo Москва;?></td> <td><?= $replace_value('destination',$key) ? $replace_value('destination', $key) : "<b>" .'Обновл.'. "</b>"; ?></a></td> <td><span class="title"><img height="50" width="120" src="http://pics.avs.io/180/70/<?= $row['airline']?>.png" /></span></td> <td><?php echo $replace_value('price', $row['price']); ?></a></td> <td><?php echo $replace_value('departure_at', substr($row['departure_at'], 0, 10)); ?></a></td> <td><?php echo $replace_value('return_at', substr($row['return_at'], 0, 10)); ?></a></td> <td><a rel="nofollow" alt="авиабилеты Москва <?php echo $replace_value('destination',$key); ?>" title="авиабилеты Москва <?php echo $replace_value('destination',$key); ?>" href="http://avia.aviaciya.com/searches/new?origin_iata=MOW&destination_iata=<?=$key?>&depart_date=<?=substr($row['departure_at'], 0, 10)?>&return_date=<?=substr($row['return_at'], 0, 10)?>&adults=1&children=0&infants=0&trip_class=0&marker=87111&with_request=true">Поиск</a></td> </tr> <?php } } } } ?> 

There's an answer:

 {"success": true, "data": {"AAH":{"1":{"price":17420,"airline":"KL","flight_number":904,"departure_at":"2017-04-17T17:00:00Z","return_at":"2017-04-25T15:15:00Z","expires_at":"2016-11-30T11:36:28Z"} 

From this answer, I need to output this "AAH" in its original form Ps / In response, many such values. Thank you in advance

  • 3
    you can work with arrays? json_decode (string, true) and you have an array. And if you can work with objects, then it is possible without true; - binliz
  • Binliz, I do not know how, I got the code in this form, it translates the city code into a normal human form, but I need this line <td> <span class = "title"> <img height = "50" width = "120 "src =" pics.avs.io/180/70/<?= $ row ['airline']?>. png "/> </ span> </ td> so that the airport code is inserted instead of the airport code, in which the answer is Json - Arcadiy Nov.
  • one
    ** foreach ($ products ['data'] as $ key => $ data) ** you have a pass through the keys and schedules. So $ key just contains AAH - binliz
  • binliz, tried it like this <img height = "50" width = "120" src = " pics.avs.io/180/70/<?= $ key ['destination']?>. jpg" /> does not work - Arcadiy
  • $ key you have AAH is not an array but a key because $ key ['destination'] is most likely null you need to enter exactly $ key - binliz

1 answer 1

If we talk specifically about the picture

 http://pics.avs.io/180/70/<?= $row['airline']?>.png 

it is necessary to replace **$row['airline']** with $key

  • Thank you so much - Arcadiy