如何阅读特定项目的数量? 例如:此ID中有多少项可用?:927007517
代码:
<?php
$content = file_get_contents('http://steamcommunity.com/profiles/76561198131798299/inventory/json/730/2');
$result = json_decode($content);
if($result->success !== true) {
echo 'Nicht erfolgreich! :(';
return;
}
foreach($result->rgInventory AS $item) {
echo $item->classid.'<br />';
}
?>
输出:http://csgo.square7.ch/csgo-master/api.php
谢谢。 最诚挚的问候
答案 0 :(得分:0)
您可以使用array_count_values
函数计算值返回的次数。这将创建一个新数组,其中id为键,计数为value。
像这样:
$count = array_count_values($result->rgInventory);
echo $count['927007517'] // Will echo the number of times this id is found
答案 1 :(得分:0)
您不能使用array_count_values,但可以编写简单的计数器
$content = file_get_contents('http://steamcommunity.com/profiles/76561198131798299/inventory/json/730/2');
$result = json_decode($content);
if($result->success !== true) {
echo 'Nicht erfolgreich! :(';
return;
}
$count = [];
foreach($result->rgInventory as $item) {
array_key_exists($item->classid, $count) ? ++$count[$item->classid] : ($count[$item->classid] = 0);
}
echo $count[927007517];