1

I'm trying to parse JSON data in the format [{code:SE rate:1.294},{code:UK rate:2.353}] from this page: http://www.mycurrency.net/service/rates

I have implemented an IP reader that detects the users location in a 2 letter country code. I want to pluck the correct data from that link with 'code' and return the value 'rate'. I was thinking I might have to do a foreach loop to iterate through all the countries?

5
  • 1
    Possible duplicate of How do I extract data from JSON with PHP? Commented Aug 12, 2017 at 13:20
  • Plus you haven't posted any code with your question, so that makes it unclear what you're asking. Commented Aug 12, 2017 at 13:23
  • by the way, the data from http://www.mycurrency.net/service/rates is not accurate, its shows my country Indonesia as "East Timor" but the currency was IDR (Indonesian Rupiah) while East Timor/TP currency is USD Commented Aug 12, 2017 at 14:01
  • @RajdeepPaul Honestly, I didn't know where to start, this is all new to me. Commented Aug 12, 2017 at 15:51
  • @MuhammadIbnuh Good spot, it's the best freesource data I could find, maybe I can convert that within my script Commented Aug 12, 2017 at 15:52

1 Answer 1

2

This is my code, I hope this is what are you looking for.

First I create a new array $output to make it more easy to search

$string = file_get_contents("http://www.mycurrency.net/service/rates");
$json = json_decode($string, true);

foreach ($json as $key => $data) {
    $output[$key]['code'] = $data['code'];
    $output[$key]['rate'] = $data['rate'];
}

After that we use a function to search value in array and returning the key. I got it from here

function searchForRate($countryCode, $array) {
   foreach ($array as $key => $val) {
       if ($val['code'] === $countryCode) {
           return $key;
       }
   }
   return null;
}

and then I run the function with the first parameter as country code to get the keys of specific country code.

$find = searchForRate("BT", $output);

And then echo the rates from our $output array by key in $find variable

echo 'RATE = '.$output[$find]['rate'];

This is the complete codes

<?php

$string = file_get_contents("http://www.mycurrency.net/service/rates");
$json = json_decode($string, true);

foreach ($json as $key => $data) {
    $output[$key]['code'] = $data['code'];
    $output[$key]['rate'] = $data['rate'];
}

function searchForRate($countryCode, $array) {
   foreach ($array as $key => $val) {
       if ($val['code'] === $countryCode) {
           return $key;
       }
   }
   return null;
}

$find = searchForRate("BT", $output);
echo 'RATE = '.$output[$find]['rate'];

Example output:

RATE = 64.13
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this works, I don't quite understand the first part does it reduce the arrays to only include code and rate?
I don't know about the performance, just personal preference. You can skip that step and continue searching on $json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.