复杂的 JSON 处理 - PHP

3

我正在使用这个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个元素。


那么你遇到了什么问题? - viral
@Viral,我刚刚修改了问题,提供了更多我想要的具体细节。 - MitchCool1
3个回答

2
也许你应该反过来做,从查看所有rgInventory出现开始启动进程。
一旦你有了一个rgInventory对象,那么
rgInventory->classid . '_' . $rgInventory->instanceid

实际上,这是与此库存项目匹配的rgDescriptions发生的关键。

因此,

foreach ( $json->rgInventory as $key => $inventory ) {

    $descKey = $inventory->classid . '_' . $inventory->instanceid

    if ( $json->rgDescriptions[$descKey]->tradable == 1 ) {

        // do whatever you want

    }
}

这个方法可行。它与我实际使用的Cetver的答案类似。虽然它与你的有点相反,但我发现它与我的代码其他部分更配合。 - MitchCool1
你用那么复杂的代码,而不是像这样简单明了的代码,你的选择。 - RiggsFolly
不,我并没有简单地复制他的代码。我使用了先遍历rgDescriptions的示例,当需要时再查看rgInventory并比较classIDs。就像我说的那样,它与你的代码类似,但方向相反。 - MitchCool1
1
是的,但库存是主记录,描述是副记录。名称本身就表明了这一点。而且为每个描述搜索整个库存数组比直接基于键访问要慢得多。 - RiggsFolly

1
$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);

1

虽然我相信RiggsFolly的回答是正确的,但这并不完全符合您的要求:
您可以将数据存储在关系型数据库中,而无需在php脚本内部重新排列数据。

create table rgInventory (
  id int,
  instanceid int,
  classid int,
  ...
)
create table rgDescriptions (
  instanceid int,
  classid int,
  ...
)

只需遍历 $json->rgInventory 并将每个项目按原样添加到相应的表中,然后对 $json->rgDescriptions 做同样的操作。
稍后您可以通过实例ID(可能还包括classid)的 JOIN 查询来查询数据。
SELECT
  ...
FROM
  rgInventory as inv
LEFT JOIN
  rgDescriptions as desc
ON
  inv.classid=rgDescriptions.classid
  AND inv.instanceid=rgDescriptions.instanceid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接