Answer the question
In order to leave comments, you need to log in
The loop goes through several times, where is the error?
There is an API code
<?php
$cache_ttl = 21600; // время жизни кэша в секундах
$cache_file_airlines = "tmp/ish.data";
$cache_file_products = "tmp/ish1.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('https://site.ru/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/aviasales/v3/prices_for_dates?origin=MOW&destination=AER&token=****&departure_at=" . date('Y-m-d') );
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);
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;
} ?>
<div id="sochi" class="container" style="margin-top: 20px;">
<div style="margin: 0px auto;padding: 10px;text-align: center;"><h2>Билеты из Москвы в Адлер</h2><br><span style="font-size: 15px!important; font-weight: 400; color: #777777!important; font-weight: 300;"></span>
</div>
<div class="table-responsive" style="overflow-x:hidden;background: var(--responsiv-color)!important; display: flex; flex-direction: column; width: 100%;margin-left: auto; margin-right: auto; list-style-type: none;overflow-y:hidden;">
<?php
if (isset($products['data']) && is_array($products['data']))
{
foreach ($products['data'] as $key => $data)
{
foreach ($data as $destination => $key)
{
if (preg_match('/[A-Z]{3}/i', $key))
{
?>
<ul class="ticket">
<li class="tickets_name"><p style="color:black;">Москва Сочи (Адлер)</p> <span class="tickets_span">Рейс: <?=$data['flight_number'] ?> MOW-AER</span></li>
<li class="li"><p><img width="100" height="50" alt="авиакомпания на рейс Москва - Адлер" src="//img.io/100/40/<?=$data['airline'] ?>.jpg" class="lazyload air"/></p><span class="tickets_span"> Количество пересадок: <?=$data['transfers'] ?></span></li>
<li class="li_span"><p style="color:black;"><?php echo $replace_value('departure_at', substr($data['departure_at'], 0, 10)); ?></p><span class="tickets_span">Туда</span></li>
<a class="tickets_link" rel="nofollow" role="button" alt="авиабилеты Москва <?php echo $replace_value('destination', $key); ?>" title="Москва <?php echo $replace_value('destination', $key); ?>" href="https://site.ru/flights/?origin_iata=MOW&destination_iata=<?=$data['destination'] ?>&depart_date=<?=substr($data['departure_at'], 0, 10) ?>&marker=87111&with_request=true&search_id=<?=$data['search_id']?>&signature=<?=$data['signature']?>"><li>Билеты от <?php echo $replace_value('price', $data['price']); ?> р. </li></a>
</ul>
<?php
}
}
}
}
?>
{"success":true,"data":[{"origin":"MOW","destination":"AER","price":2699,"airline":"DP","flight_number":"121","departure_at":"2021-07-21T14:35:00+03:00","search_id":"324813fa-a7a3-4acc-b321-285767bca79b","signature":"f6afb19d9382744abec928d70c224e3a","transfers":0,"return_transfers":0}],"currency":"rub"}
<?php
if (isset($products['data']) && is_array($products['data']))
{
foreach ($products['data'] as $key => $data)
{
foreach ($data as $destination => $key)
{
if (preg_match('/[A-Z]{3}/i', $key))
{
?>
Answer the question
In order to leave comments, you need to log in
Judging by your JSON received from the service, you do not need two foreach loops, the second one is superfluous, you can simply delete it.
you will have at least 2 pregmatch strings, in your case "origin": " MOW ", "destination": " AER ", so there will most likely always be duplicates.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question