Page 1 of 1

php 5: xml to array?

Posted: Fri Jan 30, 2009 7:03 pm
by Citizen
When I'm forced to be compatible to php 4, I use a long xml2array custom function, but by the grace of god this time I get to use php 5. I know there's new functions and I've read the manual over several times but I'm hoping there's a simple solution for what I'm looking for. Everything on the manual looks like overkill for what I need.

All I need to do is turn this xml (from a file in this case):

Code: Select all

<a>
<b>1</b>
<c>2</c>
</a>
Into an array so that :

Code: Select all

echo $array['a']['b'];
Would print '1'

Re: php 5: xml to array?

Posted: Fri Jan 30, 2009 7:53 pm
by requinix
Does it have to be an array? SimpleXML gives you a simple syntax with $var->b.

(<a> is the top-level element. Since it's required many XML readers just ignore it. Thus the statement is ->b and not ->a->b.)

Re: php 5: xml to array?

Posted: Sat Jan 31, 2009 10:11 am
by Citizen
Thanks, I ended up using simplexml_load_file() and it did the job.