我想要做的是基本上選擇一個數組內的數組並打印它想打印的內容。這是數組。PHP數組,選擇打印一個特定的元素及其內容
{"success":true,"rgInventory":{"3480848569":{"id":"3480848569","classid":"991959905","instanceid":"0","amount":"1","pos":1},"3480576615":{"id":"3480576615","classid":"937248085","instanceid":"188530139","amount":"1","pos":2},"3480546604":{"id":"3480546604","classid":"1289504012","instanceid":"188530139","amount":"1","pos":3},"3480019616":{"id":"3480019616","classid":"1287503583","instanceid":"188530139","amount":"1","pos":4},"3480018692":{"id":"3480018692","classid":"1287539933","instanceid":"188530139","amount":"1","pos":5},"3468858273":{"id":"3468858273","classid":"310796325","instanceid":"188531406","amount":"1","pos":6},"3461024351":{"id":"3461024351","classid":"1285429560","instanceid":"0","amount":"1","pos":7},"3458814606":{"id":"3458814606","classid":"1285140836","instanceid":"188531307","amount":"1","pos":8},"3456892821":{"id":"3456892821","classid":"1283034897","instanceid":"188530398","amount":"1","pos":9},"3446207634":{"id":"3446207634","classid":"1280315186","instanceid":"0","amount":"1","pos":10},"3243469958":{"id":"3243469958","classid":"1232873338","instanceid":"480085569","amount":"1","pos":11},"3144270103":{"id":"3144270103","classid":"1290987654","instanceid":"1210747767","amount":"1","pos":12}}
我想打印「rgInventory」和陣列中的我想不僅打印的主要項目,但也有些人裏面的內容SICH id和CLASSID。
這是我的代碼。
<?php
if(empty($_SESSION['steamid'])){
echo 'Please sign in to view your items.';
}else{
$id = $steamprofile['steamid'];
$key = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';
if($id != null){
$link = file_get_contents('http://steamcommunity.com/profiles/'.$id.'/inventory/json/730/2');
$inventory = json_decode($link, true);
//print_r ($inventory);
print(sizeof($inventory));
foreach($inventory as $item => $id){
for($x = 0; sizeof($item[1]) <= $x; $x+=0){
echo 'ID: '.$id;
}
}
}
}
?>
這是我只是想弄明白這是如何工作的。數組讓我困惑,所以任何解釋都會有所幫助,值得讚賞。
'$ steamprofile'從哪裏來?它從來沒有在你的代碼中設置。另外,JSON似乎是無效的......你從不檢查'json_decode'的返回值。 –
@ SverriM.Olsen'$ steamprofile'包含在其他地方,並且json是有效的,你只是不能恢復它,除非你有蒸汽id(在我的網站上登錄並存儲在'$ steamprofile'中時給出。該陣列如此經常只是短路通過它提及 然而,如果你必須在這裏看到它是這樣的:http://steamcommunity.com/profiles/76561197994925827/inventory/json/730/2 – mtcamesao
你的json數組 –