所以我有一段JSON数据。记住:我正在展示作品!
{
"status" : "success",
"prices" : [
{
"market_hash_name" : "AK-47 | Aquamarine Revenge (Battle-Scarred)",
"price" : "15",
"created_at" : 1460708937
},
这是我的PHP,它应该得到价格,但它不起作用。可能是什么问题? 'price'=>$prices->prices[0]->market_hash_name[$row['market_hash_name']]->price);
答案 0 :(得分:0)
因为您在 market_hash_name 节点下没有价格。 这应该可以解决问题:
//the second parameter (true) says that the function should return an array
$data = json_decode($jsonString, true);
echo $data['prices'][0]['price'];
重要说明:这只是使用给定json字符串部分的示例。您必须自己索引节点。 :)
答案 1 :(得分:0)
查看答案:
在线链接:3v4l.org
主JSON数据太大,无法在任何Web服务器中运行 沙箱或3v41。
有json
的部分。
$market_hash_name = 'AK-47 | Aquamarine Revenge (Battle-Scarred)';
$json = '{
"status" : "success",
"prices" : [
{
"market_hash_name" : "AK-47 | Aquamarine Revenge (Battle-Scarred)",
"price" : "15",
"created_at" : 1460708937
},
{
"market_hash_name" : "AK-47 | Aquamarine Revenge (Factory New)",
"price" : "41.4",
"created_at" : 1460708934
}
]
}';
$arr = json_decode($json, true);
foreach($arr['prices'] as $key => $val){
if($val['market_hash_name'] == $market_hash_name){
echo $val['price']; //15
}
}
答案 2 :(得分:0)
您需要使用$row['market_hash_name']
迭代价格以找到匹配的 market_hash_name 。然后,您可以从该匹配对象中提取价格。
我尽量让你的代码尽可能接近。
$sql = $db->query('SELECT * FROM `items`');
$prices2 = file_get_contents('cache/SuiPrices.json');
$prices2 = json_decode($prices);
while ($row = $sql->fetch()) {
$price = null;
foreach($prices2->prices as $data) {
if($row['market_hash_name'] == $data->market_hash_name) {
$price = (float) $data->price;
break;
}
}
$array['items'][] = array(
'img' => 'http://steamcommunity-a.akamaihd.net/economy/image/'.$row['img'].'/200fx200',
'name' => $row['market_hash_name'],
'assetid' => $row['assetid'],
'price' => $price
}
}
exit(json_encode($array));