sorry, but i'm a little bit convused... The properties of the simpleXMLElement, created by the simplexml_load_string
method, returns always an one-dimensional array with exactly one . It's no matter if i use one, more or no index - it's all the same result... the only exception is echo:
$xml = simplexml_load_string("<main><name>Alfred</name></main>");
echo $xml->name;
//-> Alfred
print_r($xml2->name);
// -> SimpleXMLElement Object ( [0] => Alfred )
print_r($xml2->name[0]);
// -> SimpleXMLElement Object ( [0] => Alfred )
print_r($xml2->name[0][0]);
// -> SimpleXMLElement Object ( [0] => Alfred )
$arr['name'] = $xml2->name;
echo(json_encode($arr));
//-> {"name":{"0":"Alfred"}}
And if i use the attribute notation, i get null:
$arr['name'] = $xml2['name'];
echo(json_encode($arr));
//-> {"name":null}
But if i use any string method, i get a clear string:
$arr['name'] = $xml2->name."";
echo(json_encode($arr));
//-> {"name":"Alfred"}
$arr['name'] = strtolower($xml2->name);
echo(json_encode($arr));
//-> {"name":"alfred"}
So, can me anybody explain this behavior? Should i just use strval() or what's the correct or most beautyfull way to get the property as string?