0

I have got this code:

$url = "http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=".$name;
$url = file_get_contents($url);
echo $url;

The output of this is

{
    "success":true,
    "lowest_price":"4,20\u20ac",
    "volume":"4,855",
    "median_price":"4,16\u20ac"
}

and I want just the lowest_price. How can i select it?

Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
VeloFX
  • 638
  • 2
  • 9
  • 16
  • 5
    Possible duplicate of [How do I extract data from JSON with PHP?](http://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – wazelin Jun 24 '16 at 13:07

3 Answers3

1

The output you're getting is called JSON String. You can parse this into array using json_decode().

<?php
     $JSON_STRING="{"success":true,"lowest_price":"4,20\u20ac","volume":"4,855","median_price":"4,16\u20ac"}";
     $array=json_decode($JSON_STRING,true);

Above code converts the Json String into array and you can access lowest_price like this (just like you access any value in array),

<?php
    echo $array["lowest_price"];

Second parameter in json_decode() denotes to convert the JSON String to Array, by default it returns PHP Object.

Reference: http://php.net/manual/en/function.json-decode.php

Alok Patel
  • 7,842
  • 5
  • 31
  • 47
1

Try

    $url = "http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=".$name;
    $url = file_get_contents($url);
    echo $url;
    $data = json_decode($url, true);
    $lowest_price = $data['lowest_price'];
    echo $lowest_price;
Ernestyno
  • 797
  • 1
  • 10
  • 16
  • I don't think this will work, on line 5 you'll need to do $lowest_price->{'lowest_price'} because json_decode by default returns an object array and not an assoc array. And why are you using the same variable name and implicitly overwriting $lowest_price, just looks confusing to me. – Wadih M. Jun 24 '16 at 14:31
  • oh, i forgot : json_decode($url, true); true means make an array :) – Ernestyno Jun 24 '16 at 14:33
0

Here's how I would do it:

$url = "http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=".$name;
$data = file_get_contents($url);
$json = json_decode($data);
$lowest_price = $json->{'lowest_price'};
echo $lowest_price;
Wadih M.
  • 12,810
  • 7
  • 47
  • 57