简体   繁体   English

计算数组中的项目数

[英]Count number of items in array

This is my code. 这是我的代码。 I am trying to get number of trade offers through steam API. 我正在尝试通过Steam API获得大量贸易报价。 I can get an array but i dont know how to count number of items from this array 我可以得到一个数组,但我不知道如何从该数组中计算项目数

<?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>';

How can I get the number of [trade_offers_recived] ? 如何获得[trade_offers_recived]的数量? Currently there are 2 trade offers recived (0 and 1) 目前有2个交易要约(0和1)

Function count can be used even for sub-array of your array. 函数count甚至可以用于数组的子数组。

So if you need count of elements in a sub-array, which is under key 'trade_offers_recived' which is in turn under key response : 因此,如果您需要在子数组中的元素计数,该子数组位于键'trade_offers_recived'下,而键'trade_offers_recived'位于键response

echo count($data['response']['trade_offers_received']);

As you have linked above your array looks like this: 当您在数组上方链接时,如下所示:

Array
(
    [response] => Array
        (
            [trade_offers_received] => Array
                (
                    [0] => Array
                    ...

So you just need to do the count on the trade_offers_received key: 因此,您只需要对trade_offers_received键进行计数:

<?php
print count($data['response']['trade_offers_received']);

Your $data array contains "response" arrays, and these "trade_offers_received" arrays. 您的$ data数组包含“ response”数组和这些“ trade_offers_received”数组。 So try this: 所以试试这个:

$numtradeoffers = count($data['response']['trade_offers_received']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM