Search code examples
phpsimplexml

Use special characters in object property name


After I have parsed an XML file using SimpleXml I need to access to the parsed file but:

print_r($xml->reservation-details);

it returns me this notice:

Use of undefined constant details - assumed 'details'

while doing this:

print_r($xml->items);

I have no problem because the property name items has no special character inside.

How can I solve, considering that the xml tag is reservation-details and I cannot modify it?


Solution

  • by using variables variable as follows :

    print_r($xml->{"reservation-details"});
    

    consider the following example:

    $ar = json_decode('{"var": "message", "var-1": "yello"}');
    print_r($ar->{"var-1"});
    

    Output : yello

    live demo https://3v4l.org/ICTGZ