我要做的是基本上在数组中选择一个数组并打印我想要打印的内容。这是阵列。
RAT 56.367,41 0,00 0,00 0,00 56.367,41
我希望打印“rgInventory”并在该数组中我不仅要打印主要项目,还要打印其中的一些内容作为id和classid。
这是我的代码。
{"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}}
这只是我想弄清楚它是如何工作的。数组让我感到困惑,所以任何解释都会有所帮助,并且非常感激。
答案 0 :(得分:1)
我明白了。
if ($inventory['success']) { print "SUCCESS = TRUE<br>"; }
foreach ($inventory['rgDescriptions'] as $rgDescription) {
for($i = 0; sizeof($rgDescription['market_name']) > $i; $i++){
if(isset($rgDescription['market_name'])){
print '<td>'.$rgDescription['market_name'].'</td>';
}
}
}
感谢Dean指出我正确的方向。
答案 1 :(得分:0)
获取json字符串并在末尾添加一个花括号,然后可以使用json_decode
获取数据e.g。
$link = '{"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}}}';
$inventory = json_decode($link, true);
if ($inventory['success']) { print "SUCCESS = TRUE<br>"; }
foreach ($inventory['rgInventory'] as $rgInventory) {
foreach ($rgInventory as $key => $value) {
print "$key=$value, ";
}
print "<br>";
}
这将输出:
SUCCESS = TRUE
id=3480848569, classid=991959905, instanceid=0, amount=1, pos=1,
id=3480576615, classid=937248085, instanceid=188530139, amount=1, pos=2,
id=3480546604, classid=1289504012, instanceid=188530139, amount=1, pos=3,
id=3480019616, classid=1287503583, instanceid=188530139, amount=1, pos=4,
id=3480018692, classid=1287539933, instanceid=188530139, amount=1, pos=5,
id=3468858273, classid=310796325, instanceid=188531406, amount=1, pos=6,
id=3461024351, classid=1285429560, instanceid=0, amount=1, pos=7,
id=3458814606, classid=1285140836, instanceid=188531307, amount=1, pos=8,
id=3456892821, classid=1283034897, instanceid=188530398, amount=1, pos=9,
id=3446207634, classid=1280315186, instanceid=0, amount=1, pos=10,
id=3243469958, classid=1232873338, instanceid=480085569, amount=1, pos=11,
id=3144270103, classid=1290987654, instanceid=1210747767, amount=1, pos=12,
您可以使用在线工具检查json编码,例如http://json.parser.online.fr/
此外,当您正在处理各种阵列时,您可以使用var_dump()
e.g。
var_dump($rgInventory);
将显示数组的组成,以便您知道如何获取所需的部分。