Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complicated Json processing - PHP

Tags:

json

php

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

I am sorting through the "rgDescriptions" data with 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 "rgDescriptions" contains a lot of information on a certain item. For each item I check some things, for example

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

I now want to also use "rgInventory" from the JSON as well, and save the information in a database with the info from "rgDescriptions". Because I'm doing checks with the"rgDescriptions" data, not all items are saved into the database, so i can't just go through all the 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: while I'm going through $mydata, I also, for some of the elements in $mydata, want to get corresponding elements from $mydata2. So If I'm at the 5th element in $mydata and I want to use $mydata2, I want the 5th element from $mydata2 as well.

like image 527
MitchCool1 Avatar asked Jul 04 '26 06:07

MitchCool1


2 Answers

Maybe you should do this the other way round and start the process by looking through all the rgInventory occurances.

Once you have a rgInventory object, the

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

is in fact the key to the occurance of rgDescriptions that matches 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

    }
}
like image 88
RiggsFolly Avatar answered Jul 05 '26 20:07

RiggsFolly


$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);
like image 33
cetver Avatar answered Jul 05 '26 20:07

cetver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!