json_decode with double array



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

我正在使用

蒸汽 api 事物 atm,我无法进行以下工作:

我可以访问带有描述的 json,但我也需要访问资产。第一个资产有一个资产 ID,用于描述中的第一项。希望你理解我xD

现在我需要将两者结合起来,以便我可以为每个项目提供一个资产 ID。

这是我的 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 与正确的项目组合在一起。

谢谢!

您可以使用

for循环而不是foreach

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

我假设"assets"和"description"数组的大小相同。

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对象的数组。每个对象都有一个资产属性和一个描述属性。您应该能够像这样访问

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

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

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

最新更新