2017-04-03 54 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