I have an xml document with tags that have two dashes in them, like so: <item--1>
. I'm using SimpleXML to parse this document, so it gives me object properties with the name of the tag. This apparently is a problem, I guess because dashes are invalid characters for variable and property names.
<?php
$xml = "<data><fruits><item>apple</item><item--1>bananna</item--1></fruits></data>";
$xml = simplexml_load_string($xml);
foreach( $xml->children() as $child ) {
var_dump($child->item);
# var_dump($child->item--1);
}
When you run this, you get
object(SimpleXMLElement)#5 (1) {
[0]=>
string(5) "apple"
}
But if you uncomment the last line, the xml element with two dashes, you get an error:
PHP Parse error: syntax error, unexpected T_LNUMBER in test.php on line 17
I tried using curly braces:
var_dump($child->{item--1});
But that only gave me this error:
PHP Parse error: syntax error, unexpected T_DEC
which is the decrement operator, or --
.
How can I reference the properties on this object?