How do I get the value of each currency for example "EUR" from this string with PHP?
{"EUR":"40.00","GBP":"35.00","USD":"45.00"}
So I get something like:
$price = 40.00
How do I get the value of each currency for example "EUR" from this string with PHP?
{"EUR":"40.00","GBP":"35.00","USD":"45.00"}
So I get something like:
$price = 40.00
This looks like JSON... it can be decoded to an PHP object or array with json_decode()
$json = '{"EUR":"40.00","GBP":"35.00","USD":"45.00"}';
$data = json_decode($json, true);
print_r($data);
$price = $data['EUR'];
var_dump($price);
JSON (JavaScript Object Notation) is a type of data format, it's publicly used in web APIs and data transmission due to its easy coupling with frontend JavaScript to take the string, and turn it straight back into an object using JSON.parse(jsonString)
(JavaScript).
In PHP, to convert this into a PHP array, the function json_decode
exists to do so. Example:
$jsonString = '{"EUR":"40.00","GBP":"35.00","USD":"45.00"}';
$phpArray = json_decode($jsonString);
Now, you can access the array as if it were the string, and you may want to use print_r
to check this further.
To get your EUR variable out, use the following to take the string, decode it, and grab the price.
$jsonString = '{"EUR":"40.00","GBP":"35.00","USD":"45.00"}';
$jsonArray = json_decode($jsonString);
$euro = $jsonArray['EUR'];
$pound = $jsonArray['GBP'];
$usdollar = $jsonArray['USD'];
Good luck in whatever financial project this may be going towards.