Complex Json Processing - PHP

I am using this JSON: http://steamcommunity.com/id/mitch8910/inventory/json/730/2/

I am sorting the "rgDescriptions" data like this:

$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
}

      

Each section in the "rgDescriptions" section contains a lot of information about a specific element. For each element, I check some things like

if($mydata->tradable == 1){ //do somthing }

      

Now I want to also use "rgInventory" from JSON and also store information in a database with information from "rgDescriptions". Since I am doing validation against the "rgDescriptions" data, not all items are saved in the database, so I cannot just go through all items in "rgInventory" and save them.

I want to do something like:

    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;
           }
        }
    }

      

EDIT: So my problem is, while I am going through $ mydata, I also want to get the corresponding items from $ mydata2 for some items in $ mydata. So if I'm in the 5th item in $ mydata and I want to use $ mydata2, I want the 5th item to be from $ mydata2 too.

+3


source to share


3 answers


$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


source


You might want to do it the other way around and start the process by looking at all rgInventory events.

Once you have an rgInventory object,

rgInventory->classid . '_' . $rgInventory->instanceid

      



is actually the key to the occurrence of the rgDescriptions that corresponds to this inventory item.

So

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

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

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

        // do whatever you want

    }
}

      

+2


source


Not exactly what you are asking for (I believe RiggsFolly answers correctly), but:
Instead of reordering the data in your php script, you can store it more or less as it is in your relational database.

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

      

Just go to $ json-> rgInventory and add each as-is element to the corresponding table, then do the same for $ json-> rgDescriptions.
Later, you can query the data with a JOIN request on the instanceid (and probably the classid too).

SELECT
  ...
FROM
  rgInventory as inv
LEFT JOIN
  rgDescriptions as desc
ON
  inv.classid=rgDescriptions.classid
  AND inv.instanceid=rgDescriptions.instanceid

      

+1


source







All Articles