1

Есть код

<?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('/[A-Z]{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
}
}
}
}
?>

Есть ответ:

{"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"}

Из этого ответа мне нужно вывести вот это "AAH" в исходном виде P.s/ В ответе много таких значений. Заранее спасибо

7
  • 3
    вы же умеете работать с массивами? json_decode(строка, true) и у вас массив. А если умеете работать с объектами то можно и без true;
    – binliz
    29 ноя 2016 в 20:42
  • binliz, не умею, мне достался код в таком виде, он переводит код города в нормальный человеческий вид, но мне нужно вот в эту строчку <td><span class="title"><img height="50" width="120" src="pics.avs.io/180/70/<?= $row['airline']?>.png" /></span></td> чтобы заместо кода аэропорта вставлялся код города, в том виде в который в ответе Json
    – Arcadiy
    29 ноя 2016 в 20:47
  • 1
    **foreach ($products['data'] as $key => $data) ** у вас проход по ключам и по рассписаниям. Так вот в $key как раз содержится AAH
    – binliz
    29 ноя 2016 в 20:51
  • binliz , пробовал вот так <img height="50" width="120" src="pics.avs.io/180/70/<?= $key['destination']?>.jpg" /> не получается
    – Arcadiy
    29 ноя 2016 в 20:54
  • $key у вас AAH это не массив а ключ потому $key['destination'] скорее всего null вам надо вписать именно $key
    – binliz
    29 ноя 2016 в 20:56

1 ответ 1

2

Если говорить именно о картинке то,

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

надо заменить **$row['airline']** на $key

1
  • Спасибо огромное
    – Arcadiy
    29 ноя 2016 в 21:24

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.