所以我正在尝试制作,代码将从JSON数组中获得与ID匹配的某些部分。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response, true);
//-------------------------------------
$invIndexes = [];
foreach($json->rgInventory as $index){
$invIndexes = $index;
}
//-------------------------------------
$makearray = (array)$invIndexes;
for($id = 0;$id < count($invIndexes);$id++){
$index = $makearray[$id];
$item = $json->rgDescriptions[$json->rgInventory[$index]->classid + "_" + $json->rgInventory[$index]->instanceid];
if($item->tradeable != 1){
continue;
}
$ItemName = $item->market_hash_name;
}
var_dump($ItemName);
以下是JSON:http://pastebin.ca/3591035
$ItemName返回的是NULL
,但它不应该(至少我认为是这样)。也许有人能发现我在这里所做的错误:/
如果您在json解码$json = json_decode($response, true);
中使用true,那么它将返回一个关联数组,因此您可以像这样访问值形式数组$json['rgInventory']
而不是$json->rgInventory
要创建$invIndexes
阵列,请使用以下方法:
$invIndexes = array();
foreach($json['rgInventory'] as $index){
$invIndexes[] = $index['id'];
}
在这里,您将在您的for loo中获得$invIndexes
。如果您再次使用$json->rgDescriptions
访问值,请将其更改为$json['rgInventory']
,对于所有其他值,请使用类似于$json['rgInventory']['index']['class']
的数组键
无需此$makearray = (array)$invIndexes;
直接使用$invIndexes
$index = $invIndexes[$id];
$item = $json['rgDescriptions'][$json['rgInventory'][$index]['classid']."_".$json['rgInventory'][$index]['instanceid']];
另一个错误是,在你的$item中没有任何密钥tradeable
,它的tradable
就像这个一样使用
if($item['tradeable'] != 1){
continue;
}
$ItemName = $item['market_hash_name'];
最后var_dump($ItemName);
第二个参数true
到json_decode
告诉它将JSON对象转换为PHP关联数组,而不是PHP对象。但是使用类似$json->rgDescriptions
的语法需要$json
作为对象;对于一个数组,它应该是CCD_ 19。
因此,要么将$json
的所有用法更改为使用数组语法,要么从json_decode
中删除true
参数。后者应该更容易。
此外,这一行:
$invIndexes = $index;
应该是:
$invIndexes[] = $index;
但你可以用来代替这个循环
$invIndexes = $json->rgInventory;