samcadek Posted April 1, 2022 Share Posted April 1, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314653-steam-json-url-of-inventory-def_index/ Share on other sites More sharing options...
gizmola Posted April 1, 2022 Share Posted April 1, 2022 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 .... Quote Link to comment https://forums.phpfreaks.com/topic/314653-steam-json-url-of-inventory-def_index/#findComment-1594815 Share on other sites More sharing options...
samcadek Posted April 1, 2022 Author Share Posted April 1, 2022 already fix it but ty for your time Quote Link to comment https://forums.phpfreaks.com/topic/314653-steam-json-url-of-inventory-def_index/#findComment-1594820 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.