Connecting Tech Pros Worldwide Forums | Help | Site Map

how to use object as array like in simplexml

emmettnicholas@gmail.com
Guest
 
Posts: n/a
#1: Mar 29 '08
Hi,
I am new to PHP, and I'm trying to implement something similar to
SimpleXML. The following code demonstrates an example of what I'm
trying to achieve:

<?php
$xmlstr = <<<XML
<asdf>
<foo att="val1">bar1</foo>
<foo att="val2">bar2</foo>
</asdf>
XML;

$xml = new SimpleXMLElement($xmlstr);

echo $xml->foo . "<br>";
echo $xml->foo[0] . "<br>";
echo $xml->foo[1] . "<br>";
echo $xml->foo[1]["att"] . "<br>";
?>

The output of this code is:

bar1
bar1
bar2
val2

What type of object is $xml->foo and how can I create a similar
object? It can be accessed as an array (e.g. $xml->foo[1]). But
echoing it doesn't print "Array" as usual, but rather apparently calls
the __toString method of $xml->foo[0].

Thanks a lot.

-Emmett

Closed Thread