我正在使用此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个元素.
解决方法:
$rows = array();
$data = file_get_contents('http://steamcommunity.com/id/mitch8910/inventory/json/730/2/');
$data = json_decode($data);
$rgInventory = $data->rgInventory;
$rgDescriptions = $data->rgDescriptions;
foreach ($rgDescriptions as $rgDescData) {
if ($rgDescData->Tradable == 1) {
$classId = $rgDescData->classid;
foreach ($rgInventory as $rgInvData) {
if ($rgInvData->classid === $classId) {
$rows[] = array(
'classIdFromrgDescriptions' => $classId,
'dataFromrgInventory' => $rgInvData
);
break;
}
}
}
}
var_dump($rows);