print_r($json); 

Gives me:

 { "items_game": { "game_info": { "first_valid_class": "1", "last_valid_class": "1000", "first_valid_item_slot": "0", "last_valid_item_slot": "15", "num_item_presets": "4" }, "rarities": { "common": { "value": "1", "loc_key": "Rarity_Common", "color": "desc_common", "next_rarity": "uncommon" }, "uncommon": { "value": "2", "loc_key": "Rarity_Uncommon", "color": "desc_uncommon", "next_rarity": "rare" }, "rare": { "value": "3", "loc_key": "Rarity_Rare", "color": "desc_rare", "next_rarity": "mythical" }, 

Is it possible to get from this array only the values ​​"value" and "color"? If possible, please tell me how ... I'm trying to do it through

  foreach($json['items_game'] as $key => $item) { echo $item['value']; } 

But I immediately get two errors, I tried to change, but nothing is lost:

 llegal string offset 'items_game' invalid argument supplied for foreach() 
  • json_decode . All is parsed in stdClass , from here errors. To convert to an array, set the second argument of the function to true . And give an example of what you need to get in the end, but it is somehow unclear. - user207618
  • and unless you clearly here ru.stackoverflow.com/questions/565648/ ... did not explain the sampling process? - Alexey Shimansky
  • @ Alexey Shimansky That code in this example does not work and gives an error. - Bim Bam
  • one
    @BimBam okey, if the problem is that nothing is output during decoding, then where is the question about foreach? it is logical that it will not work .... But to guess that you find yourself in $ arr there is nothing on the code provided in the question - somehow problematic, do you not find?. The problem is one, but the question with everything about the other ... ........ and after json_decode does not display anything because apparently json is not valid .. if it is such as in the question, then 100% is invalid. - Alexey Shimansky
  • one
    @BimBam why are you converting something somewhere there, get valid json from steam api, and then proceed as it was written to you below. To get json from steam itself, add in the link &format=json , and do not torture people. - Bookin

1 answer 1

Corrected json

 { "items_game": { "game_info": { "first_valid_class": "1", "last_valid_class": "1000", "first_valid_item_slot": "0", "last_valid_item_slot": "15", "num_item_presets": "4" }, "rarities": { "common": { "value": "1", "loc_key": "Rarity_Common", "color": "desc_common", "next_rarity": "uncommon" }, "uncommon": { "value": "2", "loc_key": "Rarity_Uncommon", "color": "desc_uncommon", "next_rarity": "rare" }, "rare": { "value": "3", "loc_key": "Rarity_Rare", "color": "desc_rare", "next_rarity": "mythical" } } } } 

Convert json to an array.

 $arr = json_decode($json, true); 

Then run through the array in a loop.

 foreach ($arr['items_game']['rarities'] as $key => $value) { echo("VALUE -> " . $value['value'] . "<br>"); echo("COLOR -> " . $value['color'] . "<br>"); } 
  • It gives an error: Invalid argument supplied for foreach () - Bim Bam
  • A piece of your json is invalid, if you add closing brackets everything works. - 5f0f5
  • The fact is that completely in this code there are 500,000 lines. And check it for validity is almost impossible. Okay, thank you for everything, I will look for a mistake) - Bim Bam