zero vacpls
zero vacpls

Reputation: 115

PHP Arrays to different variables

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);

Upvotes: 1

Views: 63

Answers (1)

Don't Panic
Don't Panic

Reputation: 41810

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
}

Upvotes: 1

Related Questions