133

I have a stdClass object created from json_decode that won't return the right number when I run the count($obj) function. The object has 30 properties, but the return on the count() function is say 1.

Any ideas?

Below is an example of one of the objects. (I'm requesting the daily trend information from Twitter). If this object had more than one property, the count($obj) would equal 1.

[trends] => stdClass Object
    (
        [2009-08-21 11:05] => Array
            (
                [0] => stdClass Object
                    (
                        [query] => "Follow Friday"
                        [name] => Follow Friday
                    )

                [1] => stdClass Object
                    (
                        [query] => "Inglourious Basterds" OR "Inglorious Basterds"
                        [name] => Inglourious Basterds
                    )

                [2] => stdClass Object
                    (
                        [query] => Inglourious
                        [name] => Inglourious
                    )

                [3] => stdClass Object
                    (
                        [query] => #songsincode
                        [name] => #songsincode
                    )

                [4] => stdClass Object
                    (
                        [query] => #shoutout
                        [name] => #shoutout
                    )

                [5] => stdClass Object
                    (
                        [query] => "District 9"
                        [name] => District 9
                    )

                [6] => stdClass Object
                    (
                        [query] => #howmanypeople
                        [name] => #howmanypeople
                    )

                [7] => stdClass Object
                    (
                        [query] => Ashes OR #ashes
                        [name] => Ashes
                    )

                [8] => stdClass Object
                    (
                        [query] => #youtubefail
                        [name] => #youtubefail
                    )

                [9] => stdClass Object
                    (
                        [query] => TGIF
                        [name] => TGIF
                    )

                [10] => stdClass Object
                    (
                        [query] => #wish09
                        [name] => #wish09
                    )

                [11] => stdClass Object
                    (
                        [query] => #watch
                        [name] => #watch
                    )

                [12] => stdClass Object
                    (
                        [query] => Avatar
                        [name] => Avatar
                    )

                [13] => stdClass Object
                    (
                        [query] => Ramadhan
                        [name] => Ramadhan
                    )

                [14] => stdClass Object
                    (
                        [query] => Goodnight
                        [name] => Goodnight
                    )

                [15] => stdClass Object
                    (
                        [query] => iPhone
                        [name] => iPhone
                    )

                [16] => stdClass Object
                    (
                        [query] => #iranelection
                        [name] => #iranelection
                    )

                [17] => stdClass Object
                    (
                        [query] => Apple
                        [name] => Apple
                    )

                [18] => stdClass Object
                    (
                        [query] => "Usain Bolt"
                        [name] => Usain Bolt
                    )

                [19] => stdClass Object
                    (
                        [query] => H1N1
                        [name] => H1N1
                    )

            )
     )
Alan Storm
  • 157,413
  • 86
  • 367
  • 554
hellopat
  • 1,609
  • 2
  • 13
  • 14
  • 3
    Could you clarify your example? As you posted it, it is has one property `2009-08-21 11:05`, being an array with 20 entries, so count($trends) would rightfully return 1. – Henrik Opel Aug 22 '09 at 00:28
  • 1
    As [Steven pointed out](http://stackoverflow.com/questions/1314745/php-count-an-stdclass-object/1314754#1314754), the Object is of type stdClass, which does not implement the Countable interface, thus count() will always return 1. – Henrik Opel Aug 22 '09 at 00:35
  • Hopel, you're exactly right. If I cast the object to an array it returns the correct number! Excellent. – hellopat Aug 22 '09 at 04:18
  • you might want to accept an answer. especially geiven that you wrote in the comment that the first given answer works for you. – Andresch Serj Apr 05 '13 at 11:28

7 Answers7

280

The problem is that count is intended to count the indexes in an array, not the properties on an object, (unless it's a custom object that implements the Countable interface). Try casting the object, like below, as an array and seeing if that helps.

$total = count((array)$obj);

Simply casting an object as an array won't always work but being a simple stdClass object it should get the job done here.

Alan Storm
  • 157,413
  • 86
  • 367
  • 554
Steven Surowiec
  • 9,196
  • 4
  • 30
  • 37
  • 9
    be ware that private properties will appear in the resultant array, wich is really unexpected. – Hugo Mota Sep 27 '12 at 23:44
  • 14
    While this method works, I would not use it for implementation because future versions of PHP could cause this code to break, I much prefer Alan Strom's answer. – ars265 Jul 14 '13 at 12:32
116

The count function is meant to be used on

  1. Arrays
  2. Objects that are derived from classes that implement the countable interface

A stdClass is neither of these. The easier/quickest way to accomplish what you're after is

$count = count(get_object_vars($some_std_class_object));

This uses PHP's get_object_vars function, which will return the properties of an object as an array. You can then use this array with PHP's count function.

Alan Storm
  • 157,413
  • 86
  • 367
  • 554
  • 1
    Before PHP 7, `get_object_vars()` on an `stdClass` that resulted from casting an array to an object will yield an empty array. Casting the `stdClass` instance to array, on the other hand, works everywhere. – XedinUnknown Jan 28 '18 at 14:42
  • Works for me on PHP 5.6.30 – R. Daumann Aug 21 '18 at 07:43
8

The object doesn't have 30 properties. It has one, which is an array that has 30 elements. You need the number of elements in that array.

Rob Drimmie
  • 1,568
  • 1
  • 13
  • 15
  • 1
    Poking at the data a little more, it might be better to use the $assoc param with json_encode, which converts it to an array. That property you're getting is named from the timestamp which is going to be a right pain to access regularly. As an array you could just do count( $trends[0] ); – Rob Drimmie Aug 22 '09 at 00:35
  • I probably should have given a better example. The stdClass object has more than just the one I gave in the example. It has 32, each containing an array with 20 elements. – hellopat Aug 22 '09 at 04:19
5

There is nothing wrong with count() here, "trends" is the only key that is being counted in this case, you can try doing:

count($obj->trends);

Or:

count($obj->trends['2009-08-21 11:05']);

Or maybe even doing:

count($obj, COUNT_RECURSIVE);
Alix Axel
  • 141,486
  • 84
  • 375
  • 483
1

Just use this

$i=0;
foreach ($object as $key =>$value)
{
$i++;
}

the variable $i is number of keys.

Arash Younesi
  • 767
  • 5
  • 17
-1

Count Normal arrya or object

count($object_or_array); 

Count multidimensional arrya or object

count($object_or_array, 1); // 1 for  multidimensional array count, 0 for Default
-4

count() function works with array. But if you want to count object's length then you can use this method.

$total = $obj->length;
WaQaR Ali
  • 117
  • 10