0

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)

4
  • 3
    See php.net/count Commented Jan 31, 2016 at 17:24
  • I tryed $dataCount = count($data); echo '<pre>' . print_r($dataCount, true) . '</pre>'; . But it is not counting what i want . Commented Jan 31, 2016 at 17:26
  • You should count the field you want to count instead. Commented Jan 31, 2016 at 17:28
  • 1
    $dataCount = count($data['response']['trade_offers_received']); // is this what you want? Commented Jan 31, 2016 at 18:52

3 Answers 3

1

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

$numtradeoffers = count($data['response']['trade_offers_received']);
Sign up to request clarification or add additional context in comments.

Comments

1

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']);

Comments

1

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']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.