How to access PHP object properties that have numeric names

It’s true, it’s not like a very good practice to have numeric property names, but sometimes there’s no other way. It can come from the database, or also from a JSON that you are converting to a PHP object.

No problem, because you can use brace notation to work around this case, enclosing the string you want to access in braces and quotes.

{‘1_1’};

Another option that I like less but that you could also do is to put the name of the property in a variable. So you access the property using that variable, like this.

$prop;

Both ways would be fine in PHP, although I think the first is much cleaner and spares you from making a variable that you really don’t want for anything other than accessing the object’s property.

See also  jQuery animate(): Animating CSS properties
Loading Facebook Comments ...
Loading Disqus Comments ...