Templar
Templar

Reputation: 1871

Reading XML and storing to arrays in PHP

I have this:

$xml = simplexml_load_file('test.xml');
print"<pre>";
print_r($xml);

It printout this:

SimpleXMLElement Object
(
    [b] => SimpleXMLElement Object
        (
            [c] => SimpleXMLElement Object
                (
                    [d] => 543
                )

        )

)

but when I type echo $xml["b"]["c"]["d"]; nothing happens

Upvotes: 1

Views: 75

Answers (2)

PHPBugs
PHPBugs

Reputation: 76

Try echo $xml->b->c->d;

$xml is not array they are objects.

Upvotes: 0

ajreal
ajreal

Reputation: 47321

the print_r is kind of misleading,
actually the $xml is series/array of SimpleXmlElement objects

so

echo (int)$xml->b->c->d;  --- type casting is required

here is some reference you should take a look first

Additional to type casting,
because everything node inside the xml object is either string or int

PHP will auto convert for numeric string to integer,
however, is clearer if you provide the type hinting

var_dump($xml); --- you should see more information on the data type

Upvotes: 4

Related Questions