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、探している定義インデックスを検索する必要があります。

たとえば、すべての金属を数えたい場合は5000、 、5001、およびを探します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)
    }
}

編集

コメントのいくつかの質問に答えるには:

$item->defindexスキーマ内defindexの現在のオブジェクトの を参照しています。このforeachループでオブジェクトの反復$itemを実行しています。それぞれがループ内で繰り返されています。したがって、ループの最初のパスでは、次のようになります。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)を実行することでアクセスできます。このループのパスでは、5002`を含むに$item->defindex等しい5009が含まれているかどうかがチェックされます。そうではないので、if ブロックは実行されません。$metal_array50005001, and

そのループで何をするかについては、おそらくコードを少し変更して、次のようにします。

$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->defindex500013

        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 に答える