带有双数组的json_decode

时间:2017-04-03 20:56:09

标签: php arrays json steam

首先我是新的,英语不是我的主要语言,所以请善待=)

我正在使用steam-api-thing atm并且我无法完成以下工作:

我可以使用描述访问json,但我也需要访问资产。并且第一个资产有一个assetid,这是描述中的第一个项目。希望你能理解我xD

现在我需要将两者结合起来,以便我可以为每个项目设置一个声明..

这是我的json:

    "assets":[
        {"appid":"730",
        "contextid":"2",
        "assetid":"9700979102",
        "classid":"310783254",
        "instanceid":"480085569",
        "amount":"1"},
        {"appid":"730",
        "contextid":"2",
        "assetid":"9700979062",
        "classid":"2050552817",
        "instanceid":"188530139",
        "amount":"1"}
    ],
    "descriptions":[
        {"appid":730,
        "classid":"310783254",
        "instanceid":"480085569",
        "currency":0,
        "background_color":"",
        "icon_url":"-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgposr-kLAtl7PvRTitD_tW1lY2EqOLmMbrfqWdY781lxOiYotWkjATk_0VuY2-lLI6VegNoYwzQ8lS-lL3qgpHvusvMyncyvic8pSGK-KHzzSg",
        "icon_url_large":"-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgposr-kLAtl7PvRTitD_tW1lY2EqOLmMbrfqWdY781lteXA54vwxlXt_EptN2nzIICXcgBoaVDQ8lTow7rvjZO86c7MznUwvHYn5nqMmxKpwUYbYpGsfXk",
        "descriptions":[],
        "tradable":1,
        "actions":[],
        "name":"Desert Eagle | Urban Rubble",
        "name_color":"D2D2D2",
        "type":"Pistole (Militärstandard)",
        "market_name":"Desert Eagle | Urban Rubble (Minimale Gebrauchsspuren)",
        "market_hash_name":"Desert Eagle | Urban Rubble (Minimal Wear)",
        "market_actions":[],
        "commodity":0,
        "market_tradable_restriction":7,
        "marketable":1,
        "tags":[]},
        {"appid":730,
        "classid":"2050552817",
        "instanceid":"188530139",
        "currency":0,
        "background_color":"",
        "icon_url":"-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpopuP1FABz7OORIQJE-dC6q5SDhfjgJ7fUqWZU7Mxkh6fEpoml2Fbj-RFuY2_xLITBewVrZ1DTrgXtw7vnjJC-tJibySA3syQk-z-DyMine1-Q",
        "icon_url_large":"-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpopuP1FABz7OORIQJE-dC6q5SDhfjgJ7fUqWZU7Mxkh9bN9J7yjRqx-BZsYzv0JtSXcgA8aVqE81Lrx-bs0cLvvsjBwHRhsiVw5S2JlhGxn1gSOUW-oNgw",
        "descriptions":[],
        "tradable":1,
        "actions":[],
        "name":"P90 | Chopper",
        "name_color":"D2D2D2",
        "type":"MP (Limitiert)",
        "market_name":"P90 | Chopper (Einsatzerprobt)",
        "market_hash_name":"P90 | Chopper (Field-Tested)",
        "market_actions":[],
        "commodity":0,
        "market_tradable_restriction":7,
        "marketable":1,"tags":[]}

和我的php代码..

$InventarUrl = "http://steamcommunity.com/inventory/".$steamID."/730/2?l=german";
$inven = file_get_contents($InventarUrl);
$invenDeco = json_decode($inven);

foreach ($invenDeco->{'assets'} as $key2 => $obj2) {
    $assetID = $obj2->assetid;
}

foreach ($invenDeco->{'descriptions'} as $key => $obj) {

    $zustand = $obj->descriptions[0]->value;

    if (strpos($zustand, "Zustand:") !== false) {
        $marketPrice = file_get_contents("http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=" . urlencode($obj->market_hash_name));
        $marketPriceDeco = json_decode($marketPrice);
        $itemName = $obj->name;
        if(strpos($obj->name, "StatTrak™")) {
            $stattrak = 1;
        } else {
            $stattrak = 0;
        }
            $ItemImageLink = $obj->icon_url;
            $ItemPrice = str_replace(",",".",$marketPriceDeco->median_price);
        }
}

我只需要知道,我如何组合两个foreach数组,我可以将assetid与正确的项目结合起来。

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用for循环代替foreach

for ($i = 0; $i < count($invenDeco->assets); $i++) {
    $asset       = $invenDeco->assets[$i];
    $description = $invenDeco->descriptions[$i];
    // Do something with $asset and $description
}

答案 1 :(得分:0)

我假设&#34;资产&#34;和&#34;描述&#34;数组大小相同。

var arr = [];
for(var i = 0; i < data.assets.length; i++) {   
    var combined = new Object();
    combined.asset = data[i].asset;
    combined.description = data[i].description;
    arr.push(combined);
}

所以现在arr是一个javascript对象数组。每个对象都有一个assets属性和一个description属性。你应该能够像这样访问

var asset = arr[0].asset;
var description = arr[0].description; // etc...

我没有将两个json数组合并为1个数组,但现在每个资产和描述数据都将存储在一个对象中。我们有,

arr[0].asset.appid == arr[0].description.appid; // TRUE