0

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

{"success":true,"lowest_price":"$2.76","volume":"345","median_price":"$2.60"}

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 = "http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Chroma%20Case%20Key";
$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.

3
  • 3
    Why would you think dividing by a string would be a good idea? Commented Jul 14, 2016 at 0:32
  • The price begins with $ so it can't be parsed as a number. You need to remove the $ first. Commented Jul 14, 2016 at 0:34
  • 1
    Why do you think the problem is with json_decode? Did you try var_dump($json_data)? Commented Jul 14, 2016 at 0:35

3 Answers 3

3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome. Thank you very much. Will mark as correct as soon as Im allowed.
1

=)) I hope you joke!

// It's ok:
$steam1 = "http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Chroma%20Case%20Key";
$json = file_get_contents($steam1);
$json_data = json_decode($json, true);

// here you set $median1 = "$2.60", yeah, it's string (not float number)
$median1= $json_data["median_price"];

// here you trying to divide one string to another
$media1 = $median1 / '2.49';

It's very interesting way )))

Change last string to:

if (preg_match('/([0-9]+(?:\.[0-9]+))/', $median1, $matches)) {
    $media1 = round($matches[1] / 2.49, 2);
} else {
    $media1 = 0;
}
echo $media1 . "\n";

Good luck!

3 Comments

Wow, Big hammer - Little nut
I don't know the site steamcommunity.com, but if there are all prices in $, you can use ltrim($median1, '$') / 2.49. In my version scripts try to find any int or float value from string $median1.
I was not complaining. Looks bullet proof
1

There is a dollar in the $json_data["median_price"] so that must be removed before you can use the field in any arithmetic.

Also you need to use a number as a divisor and not a string.

This works

<?php
$steam1 = "http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Chroma%20Case%20Key";
$json = file_get_contents($steam1);
$json_data = json_decode($json, true);
$median1= $json_data["median_price"];
$median1 = substr($median1,0,1) == '$' ? substr($median1,1) : $median1;  //remove the $
$media1 = $median1 / 2.49;
echo $media1;

Comments

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.