Question

I searched around for the answer to this but couldn't find anything and was looking for help from you wonderful people. I have been toying around with steam web API for my site. I have found this code and am using it. Also, I have barely any experience in steam api, most of my experience is in C. Anyway, here is the code:

[insert_php]
$api = "http://api.steampowered.com/IEconItems_440/GetPlayerItems/v0001/?key=MyApiKey&steamid=MySteamId&format=json";
$json = (file_get_contents($api));
$schema = json_decode($json);   
print var_dump($schema); 
[/insert_php]

I am using a plugin and inserting this php into my WordPress page. This code goes into my steam backpack and gives me all the items I have in a complicated list. What I need help with is condensing it so that it can be easily read. A defining feature of the inventory items is the defindexes. What I want to do is have it so if it finds a certain amount of one item with the same defindex, it will return that amount like this into my page: Scrap Metal = # of defindexes of Scrap Metal found. I hope that this is clear enough and that there is an answer. Thank you.

Part of my code that is returned now:

{
                "id": 1828947688,
                "original_id": 1176490973,
                "defindex": 5009,
                "level": 1,
                "quality": 6,
                "inventory": 2147483650,
                "quantity": 1,
                "origin": 4
            },
            {
                "id": 1828947700,
                "original_id": 1176491289,
                "defindex": 5009,
                "level": 1,
                "quality": 6,
                "inventory": 2147483651,
                "quantity": 1,
                "origin": 4
            },
            {
                "id": 1828947742,
                "original_id": 1178541917,
                "defindex": 5009,
                "level": 1,
                "quality": 6,
                "inventory": 2147483652,
                "quantity": 1,
                "origin": 4
            },
            {
                "id": 1828947755,
                "original_id": 1178542060,
                "defindex": 5009,
                "level": 1,
                "quality": 6,
                "inventory": 2147483757,
                "quantity": 1,
                "origin": 4
            },
            {
                "id": 1828947766,
                "original_id": 1179066746,
                "defindex": 5005,
                "level": 1,
                "quality": 6,
                "inventory": 2147483653,
                "quantity": 1,
                "origin": 4
            },
            {
                "id": 1828947780,
                "original_id": 1181421843,
                "defindex": 5009,
                "level": 1,
                "quality": 6,
                "inventory": 2147483756,
                "quantity": 1,
                "origin": 4
            },
            {
                "id": 1828947788,
                "original_id": 1181426745,
                "defindex": 5006,
                "level": 1,
                "quality": 6,
                "inventory": 2147483654,
                "quantity": 1,
                "origin": 4
            },
            {
                "id": 1828947793,
                "original_id": 1187413384,
                "defindex": 5007,
                "level": 1,
                "quality": 6,
                "inventory": 2147483755,
                "quantity": 1,
                "origin": 4
            },
            {
                "id": 1828947796,
                "original_id": 1187413535,
                "defindex": 5007,
                "level": 1,
                "quality": 6,
                "inventory": 2147483655,
                "quantity": 1,
                "origin": 4
            },
            {
                "id": 1828947801,
                "original_id": 1187416362,
                "defindex": 5007,
                "level": 1,
                "quality": 6,
                "inventory": 2147483754,
                "quantity": 1,
                "origin": 4
            },
            {
                "id": 1828947810,
                "original_id": 1190342559,
                "defindex": 5013,
                "level": 1,
                "quality": 6,
                "inventory": 2147483656,
                "quantity": 1,
                "origin": 4
            },
            {
                "id": 1828947826,
                "original_id": 1190342965,
                "defindex": 5013,
                "level": 1,
                "quality": 6,
                "inventory": 2147483753,
                "quantity": 1,
                "origin": 4
            },
            {
                "id": 1828947835,
                "original_id": 1243518373,
                "defindex": 5011,
                "level": 1,
                "quality": 6,
                "inventory": 2147483657,
                "quantity": 1,
                "origin": 4
            }
ETC.
Was it helpful?

Solution

You will need to loop through each item in your result $schema and search for the defindexes you are looking for.

For example, if you want to count all of your metal, you'll be looking for 5000, 5001, and 5002.

$metal_array = array(5000, 5001, 5002);
foreach ($schema as $item)
{
    if (in_array($metal_array, $item->defindex)
    {
        // Do something for the $item. Presumably, you'll add these up 
        // So that you can get a total metal count (remember that refined is worth 9 scrap  
        // and reclaimed is worth 3)
    }
}

Edit:

To answer some of the questions in your comment:

$item->defindex is referring to the defindex of the current $item object in your schema. You are performing object iteration in this foreach loop. Each item is being iterated over in the foreach loop. So, on the first pass through the loop, $item will look like this:

        {
            "id": 1828947688,
            "original_id": 1176490973,
            "defindex": 5009,
            "level": 1,
            "quality": 6,
            "inventory": 2147483650,
            "quantity": 1,
            "origin": 4
        }

You can access any of these values by doing $item->ATTRIBUTENAME (ie. $item->quality or $item->origin). On this pass through the loop, it will check if $item->defindex, which equals 5009 is in the $metal_array, which contains 5000, 5001, and5002`. It does not, so the if block does not execute.

As for what you'd do in that loop, I'd probably modify the code slightly to do something like this:

$metal_array = array(5000, 5001, 5002);
$total_metal = 0;
foreach ($schema as $item)
{
    if (in_array($metal_array, $item->defindex)
    {
        switch ($item->defindex)
        {
            case 5000:
               $total_metal++;     // or $total_metal += 1;
               break;
            case 5001:
               $total_metal += 3;
               break;
            case 5002:
               $total_metal += 9;
               break;
        }
    }
}

At the end of this block, you will have a value in $total_metal that equals the scrap value of all of your metal.

This block uses the switch statement to determine how much a particular item is worth. The break statement within each case prevents the logic from "falling through" to the next option.

For example, if the break was not added to the end of the case 5000 block of code and an $item->defindex equaled 5000, it would add 1 and then 3. This is not the answer you want. It is, however, a valid option to consider in certain circumstances.

        switch ($item->defindex)
        {
            case 5000:
               $total_metal++;     // or $total_metal += 1;
            case 5001:
               $total_metal += 3;
               break;
         }

Finally, if you are new to PHP, I recommend taking a look at the documentation: http://php.net/manual/en/index.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top