我正在使用这个JSON:http://steamcommunity.com/id/mitch8910/inventory/json/730/2/
我正在使用以下代码筛选“rgDescriptions”数据:
$data = file_get_contents('http://steamcommunity.com/id/mitch8910/inventory/json/730/2/');
$json = json_decode($data);
foreach ($json->rgDescriptions as $mydata)
{
//checking and adding data to a database
}
“rgDescriptions”中的每个部分都包含有关特定项目的大量信息。对于每个项目,我会检查一些内容,例如
if($mydata->tradable == 1){ //do somthing }
我现在也想使用JSON中的“rgInventory”,并将信息与“rgDescriptions”中的信息保存到数据库中。由于我正在对“rgDescriptions”数据进行检查,因此并不是所有项目都保存到数据库中,因此我不能只是遍历“rgInventory”中的所有项目并将它们保存。我想做的是:
foreach ($json->rgDescriptions as $mydata)
{
if($mydata->tradable == 1){
foreach ($json->rgInventory as $mydata2){
//I want to get the elements from rgInventory that are
// in the same place as the corresponding
//elements in rgDescriptions
$id = $mydata2[$mydata]->id;
}
}
}
编辑:我的问题是:当我遍历$mydata时,对于$mydata中的某些元素,我还想从$mydata2中获取相应的元素。因此,如果我在$mydata中的第5个元素处,并且我想使用$mydata2,则我也希望从$mydata2中获取第5个元素。