PHP: Count a stdClass object

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.

Leave a Comment