How do i count the number of objects in php?

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

162k89 gold badges386 silver badges586 bronze badges

asked Aug 22, 2009 at 0:22

4

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

162k89 gold badges386 silver badges586 bronze badges

answered Aug 22, 2009 at 0:26

Steven SurowiecSteven Surowiec

9,6424 gold badges31 silver badges37 bronze badges

2

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.

answered Aug 22, 2009 at 1:05

Alan StormAlan Storm

162k89 gold badges386 silver badges586 bronze badges

2

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.

answered Aug 22, 2009 at 0:27

Rob DrimmieRob Drimmie

1,5761 gold badge13 silver badges15 bronze badges

2

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

answered Aug 22, 2009 at 0:31

Alix AxelAlix Axel

148k91 gold badges390 silver badges493 bronze badges

0

Just use this

$i=0;
foreach [$object as $key =>$value]
{
$i++;
}

the variable $i is number of keys.

answered Oct 1, 2017 at 1:12

Arash YounesiArash Younesi

1,4911 gold badge10 silver badges21 bronze badges

You can use ArrayIterator for that purpose.

[new \ArrayIterator[$obj]]->count[]

answered Mar 5 at 21:30

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

answered Feb 29, 2020 at 10:19

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

$total = $obj->length;

answered Dec 23, 2015 at 5:11

How many objects are there in PHP?

1800 objects can be created and torn down in a matter of milliseconds, repeatedly.

How does count [] work in PHP?

PHP count[] function is an in-built function available in PHP, which counts and returns the number of elements in an array. It also counts the number of properties in an object. The count[] function may return 0 for the variable, which has been declared with an empty array or for the variable which is not set.

How do I find the length of an array of objects in PHP?

The sizeof[] function returns the number of elements in an array. The sizeof[] function is an alias of the count[] function.

How do you count elements in an array?

A number of elements present in the array can be found by calculating the length of the array..
STEP 1: START..
STEP 2: INITIALIZE arr[] = {1, 2, 3, 4, 5}.
STEP 3: length= sizeof[arr]/sizeof[arr[0]].
STEP 4: PRINT "Number of elements present in given array:" by assigning length..
STEP 5: RETURN 0..
STEP 6: END..

Chủ Đề