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

我想这些项目(从下面链接的API附带)进行排序是这样的:例如:PHP数组不同的变量

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

和应重复的所有项目。所以我们会列出所有物品的价格。

现在我有这样的代码:

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

    print_r($priceList); 

回答

1

的API在你的代码返回JSON。在您完成$priceData = file_get_contents($priceSource);之后,您可以将其解码为json_decode的阵列。

$decoded = json_decode($priceData, true); 

然后你就可以在阵列上进行迭代,并做任何你需要的物品,其价格要做到:

foreach ($decoded as $item => $price) { 
    echo "<p>$item costs $price</p>";  // for example 
} 
+0

那么,如果我在我的库存称为★卡口项目|蓝色钢铁(略有磨损),只想获得该特定物品的价格。我会怎么做? –

+1

如果您知道商品的确切名称,您可以通过使用该名称作为解码数组中的关键字来获得其价格:'$ price = $ decoded ['★Bayonet |蓝钢(略有磨损)'];' –