Jump to content

steam json url of inventory def_index


samcadek

Recommended Posts

https://steamcommunity.com/profiles/76561198182213262/inventory/json/440/2 this is the file and I would like to get the def_index from rgDescriptions in descriptions in app_data

foreach ($inventories['rgDescriptions'] as $key => $description) {       
  if (isset($description['descriptions'])) {
    foreach ($description['descriptions'] as $key => $effects2) {
      if (isset($effects2['app_data'])) {
        $effectsValue2 = $effects2['app_data'];
        foreach ($effectsValue2 as $key => $treba) {
          if (isset($effectsValue2['def_index'])) {
              $effectsValue3 = $effectsValue2['def_index'];
          }
        }
      }
    }
  }
}


I made this but the respond is rly random

Link to comment
Share on other sites

There are a few concepts that will help you.

The first is the use of the empty function.  For arrays it is especially useful, in that it checks both for existence and whether or not the item actually has a value.  While you can foreach() an empty array, with empty your code can already detect that the array is empty and the foreach will never be entered.

This revised code should help you better understand the variations in the data:

 

if (!empty($inventories['rgDescriptions'])) {
    foreach ($inventories['rgDescriptions'] as $key => $description) {       
        if (!empty($description['descriptions'])) {
            foreach ($description['descriptions'] as $key => $effects2) {
                var_dump($effects2);
                
            }
        }
    }
}

 

This reveals that app_data is an array that exists in some entries, and when it does, appears to have key/value pairs.    

array(3) {
  ["value"]=>
  string(15) "    Death Stare"
  ["color"]=>
  string(6) "4b69ff"
  ["app_data"]=>
  array(1) {
    ["def_index"]=>
    string(5) "31225"
  }
}
array(3) {
  ["value"]=>
  string(24) "    Spooky Head-Bouncers"
  ["color"]=>
  string(6) "4b69ff"
  ["app_data"]=>
  array(1) {
    ["def_index"]=>
    string(5) "31209"
  }
}

 

A useful function for finding out if an array has a particular key is array_key_exists. This helps you determine if the app_data array contains a 'def_index' value or not.  There are many entries in your data that don't have one.

Putting this all together, I added code to output the item 'value' which appears to be a name, and the def_index value you wanted.  I don't have any idea what that value actually is, so I included the name to make this demonstration somewhat interesting.  Frequently the name has whitespace in it, so I used trim() to remove that.

if (!empty($inventories['rgDescriptions'])) {
    foreach ($inventories['rgDescriptions'] as $key => $description) {       
        if (!empty($description['descriptions'])) {
            foreach ($description['descriptions'] as $key => $effects2) {
                // var_dump($effects2);
                if (!empty($effects2['value']) && !empty($effects2['app_data'])) {
                    if (array_key_exists('def_index', $effects2['app_data'])) {
                        echo trim($effects2['value']) . ": {$effects2['app_data']['def_index']}" . PHP_EOL; 
                    }
                } 
            }
        }
    }
}

 

Oh Deer!: 31245
Jolly Jester: 31243
Merry Cone: 31247
Hat Chocolate: 31259
Elf-Made Bandanna: 31260
Seasonal Spring: 31244
Elf Ignition: 31253
Train Of Thought: 31254
Seasonal Employee: 31258
....

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.