How to find if an object generated by an object is empty in PHP

I am working with an object returned from a class. For some reason, the class spills out an object with numbered properties (0, 1, 2, etc.). I need to check if the object is empty. The standard empty(get_object_vars($obj)) trick empty(get_object_vars($obj)) will not work, because get_object_vars returns an empty array, even if the object has (numbered) properties.

For reference, the object I'm working with is the one that returns the PHP ZipCode legislators method for the Sunlight API. You can see print_r model answer here .

+4
source share
2 answers

Judging by the code, the author made a mistake when starting a numerical indexed array for an object. This makes it impossible to get the properties of an object by name, although you can still foreach over it anyway. You can also just return it to an array: $results = (array) $obj; . Then the count array.

+4
source

It works:

 if (current($obj) === false) echo "is empty\n"; 

It probably does implicit conversion to an array.

0
source

All Articles