Как получить по АПИ только первое значение?
Код
<?php
$cache_ttl = 21600; // время жизни кэша в секундах
$cache_file_airlines = "tmp/airlines.data";
$cache_file_products = "tmp/products.data";
$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;
};
if (
file_exists($cache_file_airlines) &&
time() - filemtime($cache_file_airlines) < $cache_ttl
) {
// берём кэшированные данные
$get_airlines = file_get_contents($cache_file_airlines);
} else {
$get_airlines = file_get_contents('/json/airlines.json');
file_put_contents($cache_file_airlines, $get_airlines);
}
$airlines = array_column($data, 'name', 'iata');
if (
file_exists($cache_file_products) &&
time() - filemtime($cache_file_products) < $cache_ttl
) {
// берём кэшированные данные
$response = file_get_contents($cache_file_products);
} else {
$ch = curl_init();
curl_setopt(
$ch,
CURLOPT_URL,
"https://api.travelpayouts.com/v1/prices/cheap?origin=MOW&destination=AER&limit=30&token=xxx"
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Access-Token: xxx"]);
$response = curl_exec($ch);
curl_close($ch);
file_put_contents($cache_file_products, $response);
}
$products = json_decode($response, true);
$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']))
{
foreach ($products['data'] as $key => $data)
{
foreach ($data as $destination => $row)
{
if (preg_match('/[A-Z]{3}/i', $key))
{
?>
Ответ API
{
"success": true,
"data": {
"AER": {
"0": {
"price": 4500,
"airline": "5N",
"flight_number": 239,
"departure_at": "2021-10-11T13:00:00Z",
"return_at": "2021-10-21T18:30:00Z",
"expires_at": "2021-02-25T17:23:00Z"
},
"1": {
"price": 4135,
"airline": "U6",
"flight_number": 273,
"departure_at": "2021-04-26T05:25:00Z",
"return_at": "2021-04-29T18:30:00Z",
"expires_at": "2021-02-24T12:42:30Z"
},
"2": {
"price": 7835,
"airline": "ZF",
"flight_number": 301,
"departure_at": "2021-04-02T05:35:00Z",
"return_at": "2021-04-03T13:05:00Z",
"expires_at": "2021-02-24T07:54:53Z"
},
"3": {
"price": 14344,
"airline": "S7",
"flight_number": 1249,
"departure_at": "2021-03-05T10:05:00Z",
"return_at": "2021-03-10T17:55:00Z",
"expires_at": "2021-02-24T05:22:45Z"
},
"4": {
"price": 32979,
"airline": "U6",
"flight_number": 447,
"departure_at": "2022-01-17T10:00:00Z",
"return_at": "2022-01-25T03:55:00Z",
"expires_at": "2021-02-23T16:25:32Z"
}
}
},
"error": "",
"currency": "rub"
}
Вот так получаю данные
<?php echo $replace_value('destination', $key); ?>
<?php echo $replace_value('departure_at', substr($row['departure_at'], 0, 10)); ?>
<?php echo $replace_value('return_at', substr($row['return_at'], 0, 10)); ?>
<?php echo $replace_value('price', $row['price']); ?>
Проблема в том, что выдаются все значение всего ответа, а мне надо одно значение самого первого, вот это:
{
"AER": {
"0": {
"price": 4500,
"airline": "5N",
"flight_number": 239,
"departure_at": "2021-10-11T13:00:00Z",
"return_at": "2021-10-21T18:30:00Z",
"expires_at": "2021-02-25T17:23:00Z"
}
}
}
Как это реализовать? В php не силен.