How to convert XML tags with attributes 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
juliasceasor
Forum Newbie
Posts: 1
Joined: Mon Jan 02, 2017 4:57 am

How to convert XML tags with attributes to array

Post by juliasceasor »

Hi,

I have a code snippet which stores the tags and value in an array (indexed). Please see below:

<foo>Test</foo>

$data['foo'] = 'Test';

But when I store the tag which has attributes as mentioned below, it does not accept.

<foo order='1'>Test</foo>

Array: $data['foo'] = array('_'=>'Test','order'=>'1'); //Referred from forums

But it sets the attributes and not actual data for the tag.

Please help me in this, how to do this.

Thanks & Regards,
Julias
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to convert XML tags with attributes to array

Post by Celauran »

juliasceasor wrote:I have a code snippet which stores the tags and value in an array (indexed).
Would help if you showed the code you were using.

That said, have you looked at SimpleXML and/or xml_parse_into_struct?
Post Reply