Mehr Mehr - 7 months ago 20
PHP Question

PHP doesn't return json decode

I have been trying to fetch some data from steam API which returns JSON such as this:


However it does not return anything to the variable which is used somewhere else. It just shows 0.

Here's my JSON code, what am I doing wrong?

$steam1 = "";
$json = file_get_contents($steam1);
$json_data = json_decode($json, true);
$median1= $json_data["median_price"];
$media1 = $median1 / '2.49';

Thanks for any help anyone can offer.


json_decode() is working fine. The problem is that you can't do arithmetic when a number begins with $.

You need to remove the $ character at the beginning of the price before you can use it as a number. Otherwise, it will be converted to 0.

$median1 = ltrim($json_data["median_price"], '$');
$media1 = $median1 / 2.49;