2017-04-03 106 views
-2

首先即時通訊新英的心不是我的主要語言,所以請善待=)json_decode雙陣列

林與蒸汽-API的事情工作ATM和我不能讓下面的工作:

我可以通過描述訪問json,但我也需要訪問資產。第一個資產有一個組合標記,並且是描述中的第一個項目。希望你能理解我由xD

現在我需要兩個結合,這樣我可以有每個項目由assetid ..

這裏是我的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結合..

謝謝!

+0

你能格式化你的json和php代碼嗎?這種方式幾乎不可讀。 –

回答

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 
} 
+0

非常感謝你! – hublrs

0

我假設「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對象數組。每個對象都有一個assets屬性和一個description屬性。你應該能夠訪問這樣

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

我沒有兩個JSON數組相當結合成1個數組,但現在每一塊資產和描述數據的將被存儲在一個對象。因此,我們有,

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

謝謝你的回答,但我需要在PHP – hublrs