0

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.
4

1 回答 1

0

您将需要遍历结果中的每个项目$schema并搜索您正在寻找的定义索引。

例如,如果您想计算所有金属,您将寻找500050015002

$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)
    }
}

编辑

要回答您评论中的一些问题:

$item->defindex指的是架构defindex中当前对象的。$item您正在此foreach循环中执行对象迭代。每个都在循环中被迭代。因此,在第一次通过循环时,将如下所示:itemforeach$item

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

$item->ATTRIBUTENAME您可以通过执行(即。$item->quality$item->origin)访问这些值中的任何一个。在这个循环中,它会检查 if 是否$item->defindex等于5009$metal_array包含500250005001, and5002 中。它没有,所以 if 块不执行。

至于你在那个循环中做什么,我可能会稍微修改代码来做这样的事情:

$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;
        }
    }
}

在此块结束时,您将获得一个$total_metal等于所有金属废料值的值。

此块使用switch语句来确定特定项目的价值。每个中的break语句case可防止逻辑“落入”下一个选项。

例如,如果break没有添加到case 5000代码块的末尾和一个$item->defindexequaled 5000,它将添加1然后3。这不是你想要的答案。然而,在某些情况下,这是一个可以考虑的有效选择。

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

最后,如果您是 PHP 新手,我建议您查看文档: http: //php.net/manual/en/index.php

于 2013-09-23T20:21:03.330 回答