Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kenprogrammer's avatar

Extract Exchange Rate from a JSON object

Below is a curl response from RapidAPI.com

Array ( [Realtime Currency Exchange Rate] => Array ( [1. From_Currency Code] => KES [2. From_Currency Name] => Kenyan Shilling [3. To_Currency Code] => USD [4. To_Currency Name] => United States Dollar [5. Exchange Rate] => 0.00948317 [6. Last Refreshed] => 2020-03-25 10:21:18 [7. Time Zone] => UTC [8. Bid Price] => 0.00947418 [9. Ask Price] => 0.00944733 ) )

Someone help me get extract the exchange rate. I have tried the code below to no avail.

$data = json_decode($response,true);
echo $data['Realtime Currency Exchange Rate']['Exchange Rate'];
0 likes
3 replies
ellgreen's avatar

Looks like you need:

$data = json_decode($response,true);
echo $data['Realtime Currency Exchange Rate']['5. Exchange Rate'];

(Added the 5. to the array key)

1 like
tykus's avatar

It looks like the key is 5. Exchange Rate, not Exchange Rate

1 like

Please or to participate in this conversation.