复杂的Json处理-PHP

我正在使用此JSONhttp://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);

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...