[UPHPU] Accessing object elements with punctuation marks
Alvaro Carrasco
alvaro at epliant.com
Thu Mar 29 12:17:06 MDT 2007
Richard K Miller wrote:
>
> On Mar 29, 2007, at 11:36 AM, Alvaro Carrasco wrote:
>
>>> But I can't access the next node down because it contains a hyphen:
>>>
>>> print_r($xml->GetDomains->domain-list); // produces error
>>> because of hyphen
>>>
>>>
>> Wrap it in curly braces:
>>
>> $xml->GetDomains->{'domain-list'}
>>
>
> Awesome, that solved that.
>
> Any idea about this?
>
> This
> <?php print_r($xml->GetDomains->{'domain-list'}); ?>
> returns this:
>
> SimpleXMLElement Object
> (
> [domain] => Array
> (
> [0] => SimpleXMLElement Object
> (
> [sld] => abc.com
> )
>
> [1] => SimpleXMLElement Object
> (
> [sld] => def.com
> )
> )
> )
>
>
> But when I type
> <?php print_r($xml->GetDomains->{'domain-list'})->domain; ?>
> it returns this:
>
> SimpleXMLElement Object
> (
> [sld] => abc.com
> )
>
>
> I would expect the latter example to return both Objects in the array
> but it's only returning the 0th. It's treating these two statements
> the same:
>
> <?php print_r($xml->GetDomains->{'domain-list'})->domain; ?>
> <?php print_r($xml->GetDomains->{'domain-list'})->domain[0]; ?>
>
>
One of the quirks of simplexml, it assumes 'grab the first one'. It's
actually intended behavior, as unintuitive as it is.
I usually prefer xpath when grabbing data from xml, it's less quirky:
$domains = $xml->xpath('/GetDomain/domain-list/domain');
Alvaro
More information about the UPHPU
mailing list