MHB2011
MHB2011

Reputation: 463

Count number of items in array

This is my code. I am trying to get number of trade offers through 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] ? Currently there are 2 trade offers recived (0 and 1)

Upvotes: 0

Views: 119

Answers (3)

hherger
hherger

Reputation: 1680

Your $data array contains "response" arrays, and these "trade_offers_received" arrays. So try this:

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

Upvotes: 1

Progrock
Progrock

Reputation: 7485

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:

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

Upvotes: 1

u_mulder
u_mulder

Reputation: 54831

Function count can be used even for sub-array of your array.

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:

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

Upvotes: 1

Related Questions