Alex Rivera | Logout

How to check if JSON object is empty in PHP?

Asked 2009-09-07T13:28:10.917
28

I'm reading JSON data with PHP and that data contains empty objects (like {}). So the problem is, I have to handle the case when object is empty in different manner but I can't find good enough way to do the check. empty(get_object_vars(object)) looks too scary and very inefficient. Is there good way to do the check?

Edit
Report

1 Answer

29

You could cast it to an array (unfortunately you can't do this within a call to empty():

$x = (array)$obj;
if (empty($x))
    ...

Or cast to an array and count():

if (count((array)$obj))
    ...
answered 2009-09-07T13:37:23.863

Your Answer