【问题标题】:Steam Inventory PHPSteam 库存 PHP
【发布时间】:2015-12-04 10:06:39
【问题描述】:
如何可能读取特定项目的数量?
例如:这个 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
谢谢。
致以最诚挚的问候
【问题讨论】:
标签:
php
steam
steam-web-api
steambot
【解决方案1】:
您可以使用array_count_values 函数计算一个值返回的次数。这将创建一个新数组,其中 id 为键,计数为值。
像这样:
$count = array_count_values($result->rgInventory);
echo $count['927007517'] // Will echo the number of times this id is found
【解决方案2】:
你不能使用 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];