Check if a json object property is available or not

A json object like

 $blah = $a->b->c->d;

this object may or may not have a “d” property. How can I check if “d” property is available or not to avoid notice?


is something valid?


Mmh… except JSON doesnt have a defined structure. stdObject wont know about properties; so… this doesnt work.

Isset works.

you’re not testing JSON. you’re testing its parsed data.

I doubt that. why shouldn’t an object doesn’t know about its properties?

Uhm… yes. Yes I am?
$a = json_decode(‘{“b”: {“c”: {“d”: 1}}}’);
So, a JSON object. Which is of type stdObject.

property_exists requires a class type as it’s first parameter. What class are you giving it to test?

Straight from the Manual:

The class name or an object of the class to test for

From the JSON Standard:

JSON is a text format that facilitates structured data interchange between all programming languages.

besides that:

gettype( json_decode('{"b": {"c": {"d": 1}}}', true) ); // array

not much of an object, if you ask me.

and yet, it still is, thanks to PHP’s typeshifting.

So, yes, you could do it with property_exists($a->b->c,‘d’). It will work exactly the same as isset, except if d is set to null {‘b’:{‘c’:{‘d’:null}}}, isset says false, property_exists says true, so take whichever fits your requirements better.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.