php 5: xml to array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

php 5: xml to array?

Post 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'
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php 5: xml to array?

Post 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.)
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Re: php 5: xml to array?

Post by Citizen »

Thanks, I ended up using simplexml_load_file() and it did the job.
Post Reply