13

I got an object (in PHP) and I can't print the content. In debug-mode it's like this:

stdClass Object
(
    [pre-selection] => 1
)

But I can't print the 'pre-selection' because of the minus sign.

echo $object->pre-selection; //doens't work.

How can I print this out? Thanks.

hakre
  • 193,403
  • 52
  • 435
  • 836
Kevin Gorjan
  • 1,272
  • 1
  • 17
  • 34
  • Possible duplicate of [getting php variable from an object in which one of its properties has $ sign](http://stackoverflow.com/questions/11871872/getting-php-variable-from-an-object-in-which-one-of-its-properties-has-sign) - There are other ones that are similar / common like yours. I still wonder where exactly this is properly documented in the PHP manual. – hakre Aug 13 '12 at 18:45
  • Another related one: [Get a PHP object property that is a number](http://stackoverflow.com/questions/9606340/get-a-php-object-property-that-is-a-number); and another one: [php curly brace object property accessing problem](http://stackoverflow.com/questions/4643894/php-curly-brace-object-property-accessing-problem) – hakre Aug 13 '12 at 18:50

2 Answers2

40

You could try

$object->{'pre-selection'};

http://php.net/manual/en/language.types.string.php#language.types.string.parsing.complex

See also Example 2 of json_decode()

Example #2 Accessing invalid object properties

Accessing elements within an object that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

<?php

$json = '{"foo-bar": 12345}';

$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345

?>

Update (thanks to salathe):

Curly braces may also be used, to clearly delimit the property name. They are most useful when accessing values within a property that contains an array, when the property name is made of mulitple parts, or when the property name contains characters that are not otherwise valid

Steve Robbins
  • 13,672
  • 12
  • 76
  • 124
  • Thanks, didn't know you could do that ;) Got it working when I putted around single-quotes. $object->{'pre-selection'}; – Kevin Gorjan Aug 13 '12 at 18:36
  • That link to the manual is about something else (however somewhat similarly written). – hakre Aug 13 '12 at 18:47
  • The syntax is mentioned in a [few](http://php.net/manual/en/function.json-decode.php#example-3471) [places](http://php.net/manual/en/simplexml.examples-basic.php#example-5116). But, it isn't mentioned under [variable properties](http://php.net/manual/en/language.variables.variable.php). – salathe Aug 13 '12 at 19:11
  • For what it's worth, I have updated the "variable properties" section to mention the curly brace syntax. (Changes will be visible next weekend after the update.) – salathe Aug 13 '12 at 19:51
7

There are multiple ways, the problem is that the PHP tokenizer will choke on the - sign in the code, howver you can write it so that the parser does not complains:

echo $object->{'pre-selection'};

or

$property = 'pre-selection'
echo $object->$property;

or

$array = (array) $object;   
echo $array['pre-selection'];

In these cases, the PHP parser does not run about a place in the raw code that it has a problem to parse with any longer.


Wondering where this is documented. For example in the SimpleXML documentation:

Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

Example #3 Getting <line>

<?php
include 'example.php';

$movies = new SimpleXMLElement($xmlstr);

echo $movies->movie->{'great-lines'}->line;
?>

The above example will output:

PHP solves all my web problems
Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Thanks, already found the solution. I added it to my question. Thanks everybody for your quick responses. – Kevin Gorjan Aug 13 '12 at 18:40
  • @KevinGorjan_ Another one is to convert into an array first. There are many ways to Rome actually. – hakre Aug 13 '12 at 18:41