zero vacpls zero vacpls - 5 months ago 33
PHP Question

PHP Arrays to diffrent 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);

Answer

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
}