这是我的代码。我试图通过steam API获得多少交易报价。我可以得到一个数组,但我不知道如何计算这个数组中的项目数
<?php
$json_url = "http://api.steampowered.com/IEconService/GetTradeOffers/v0001/?key=KEY&get_received_offers=1&active_only=1";
$json = file_get_contents($json_url);
$data = json_decode($json, TRUE);
echo '<pre>' . print_r($data, true) . '</pre>';
如何获得[trade_offers_recived]的数量?目前有2个交易报价(0和1)
您的$ data数组包含“响应”数组和这些“trade_offers_received”数组。试试这个:
$numtradeoffers = count($data['response']['trade_offers_received']);
函数count
甚至可以用于数组的子数组。
因此,如果你需要一个子数组中的元素数,这是在关键'trade_offers_recived'
下的关键response
下:
echo count($data['response']['trade_offers_received']);
正如您在上面链接的那样,您的数组如下所示:
Array
(
[response] => Array
(
[trade_offers_received] => Array
(
[0] => Array
...
所以你只需要对trade_offers_received键进行计数:
<?php
print count($data['response']['trade_offers_received']);