1
Array ( [0] => { [1] => ★ Bayonet | Blue Steel (Minimal Wear) [2] => :119.41, [3] => ★ StatTrak™ Huntsman Knife | Scorched (Well-Worn) [4] => :101.65 }

I want these items (Comes from the API linked below) to be sorted like this: Example:

ItemName = "★ Bayonet | Blue Steel (Minimal Wear)";
ItemPrice = "119.41";

And that should be repeated for all the items. So we get a name with a price for all items listed.

Right now I have this code:

$priceSource = "https://api.csgofast.com/price/all";
    $priceData = file_get_contents($priceSource);
    $priceList = explode(':',$priceData);
    $priceList = explode(',',$priceData);
    $priceList = explode('"',$priceData);

    print_r($priceList);
Cœur
  • 37,241
  • 25
  • 195
  • 267
zero vacpls
  • 115
  • 2
  • 10

1 Answers1

1

The API in your code returns JSON. After you do $priceData = file_get_contents($priceSource); you can decode it to an array with json_decode.

$decoded = json_decode($priceData, true);

Then you can iterate over the array and do whatever you need to do with the items and their prices:

foreach ($decoded as $item => $price) {
    echo "<p>$item costs $price</p>";       // for example
}
Community
  • 1
  • 1
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • So what if I have an item in my inventory called ★ Bayonet | Blue Steel (Minimal Wear) and only want to get the price for that specific item. How would I do that? – zero vacpls Jul 05 '16 at 22:53
  • 1
    If you know the exact name of the item, you can get its price by using that name as a key in the decoded array: `$price = $decoded['★ Bayonet | Blue Steel (Minimal Wear)'];` – Don't Panic Jul 05 '16 at 22:55