Hello, I have this code:

<?php $url = 'https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5'; $obj = json_decode(file_get_contents($url), true); print_r($obj); ?> 

he receives data from PrivatBank in the following form:

 Array ( [0] => Array ( [ccy] => USD [base_ccy] => UAH [buy] => 26.50000 [sale] => 26.85000 ) [1] => Array ( [ccy] => EUR [base_ccy] => UAH [buy] => 32.20000 [sale] => 32.70000 ) [2] => Array ( [ccy] => RUR [base_ccy] => UAH [buy] => 0.46000 [sale] => 0.49000 ) [3] => Array ( [ccy] => BTC [base_ccy] => USD [buy] => 10136.3718 [sale] => 11203.3583 ) ) 

I need from this in 2 variables to get the dollar to hryvnia exchange rate, that is, Chila: 26,50000 and 26.85000, so that I would end up with 2 variables $ kurs_buy $ kurs_sale. The whole brain continued, but how to get these values ​​from there I can not understand

    2 answers 2

     $url = 'https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5'; $obj = json_decode(file_get_contents($url), true); $kurs_buy = $kurs_sale = 0; foreach ($obj as $item) { if ($item['ccy'] == 'USD' && $item['base_ccy'] == 'UAH') { $kurs_buy = $item['buy']; $kurs_sale = $item['sale']; continue; } } echo "buy: {$kurs_buy} - sale: {$kurs_sale}"; 

      If I understood correctly

       $url = 'https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5'; $obj = json_decode(file_get_contents($url), true); echo 'Покупка <br>'; echo '1 USD = '.$obj[0]['buy'].' UAH <br>'; echo '1 EUR = '.$obj[1]['buy'].' UAH <br>'; echo '1 RUR = '.$obj[2]['buy'].' UAH <br>'; echo '<br> Продажа <br>'; echo '1 USD = '.$obj[0]['sale'].' UAH <br>'; echo '1 EUR = '.$obj[1]['sale'].' UAH <br>'; echo '1 RUR = '.$obj[2]['sale'].' UAH <br>'; echo '<br>'; echo '1 USD - Покупка: '.$obj[0]['buy'].' UAH, Продажа: '.$obj[0]['sale'].' UAH<br>'; echo '1 EUR - Покупка: '.$obj[1]['buy'].' UAH, Продажа: '.$obj[1]['sale'].' UAH<br>'; echo '1 RUR - Покупка: '.$obj[2]['buy'].' UAH, Продажа: '.$obj[2]['sale'].' UAH<br>';