Jump to content

[SOLVED] xml to array


benjaminbeazy

Recommended Posts

OK guys, I've been working with PHP for some time now but I have very little experience with XML related code.

 

I have access to the Pownce API which returns an XML document for the specified query and I'd like to convert that into a multidimensional array following the structure of the XML. Not sure if there is something already is PHP to do this or what. I played around a little with xml_parse but it wasn't digging into each node for subnodes and returning the whole shebang as I'd like.

 

Example XML:

<note>
<id>a number</id>
<permalink>a url</permalink>
<type>message</type>
<body>text</body>
<timestamp>1193827966</timestamp>
<seconds_since>2395</seconds_since>
<display_since>19 min ago</display_since>
<num_recipients>19</num_recipients>
<stars>0.0</stars>
<is_public>1</is_public>
<sender>
<username>someuser</username>
<permalink>a url</permalink>
<first_name>someuser's name</first_name>
<short_name>someuser's name</short_name>
<is_pro>0</is_pro>
<profile_photo_urls>
<tiny_photo_url>a url</tiny_photo_url>
<small_photo_url>a url</small_photo_url>
</profile_photo_urls>
<blurb>some text</blurb>
<location>some text</location>
<country>some text</country>
<gender>some gender</gender>
</sender>
<recipients>
<user>
<username>someuser</username>
<permalink>a url</permalink>
<first_name>someuser's name</first_name>
<short_name>someuser's name</short_name>
<is_pro>0</is_pro>
<profile_photo_urls>
<tiny_photo_url>a url</tiny_photo_url>
<small_photo_url>a url</small_photo_url>
</profile_photo_urls>
<blurb>some text</blurb>
<location>some text</location>
<country>some text</country>
<gender>some gender</gender>
</user>
</recipients>
</note>

<note>
<id>a number</id>
<permalink>a url</permalink>
<type>message</type>
<body>some text</body>
<timestamp>1193827966</timestamp>
<seconds_since>2395</seconds_since>
<display_since>19 min ago</display_since>
<num_recipients>19</num_recipients>
<stars>0.0</stars>
<is_public>1</is_public>
<sender>
<username>someuser</username>
<permalink>a url</permalink>
<first_name>someuser's name</first_name>
<short_name>someuser's name</short_name>
<is_pro>0</is_pro>
<profile_photo_urls>
<tiny_photo_url>a url</tiny_photo_url>
<small_photo_url>a url</small_photo_url>
</profile_photo_urls>
<blurb>some text</blurb>
<location>some text</location>
<country>some text</country>
<gender>some gender</gender>
</sender>
<recipients>
<user>
<username>someuser</username>
<permalink>a url</permalink>
<first_name>someuser's name</first_name>
<short_name>someuser's name</short_name>
<is_pro>0</is_pro>
<profile_photo_urls>
<tiny_photo_url>a url</tiny_photo_url>
<small_photo_url>a url</small_photo_url>
</profile_photo_urls>
<blurb>some text</blurb>
<location>some text</location>
<country>some text</country>
<gender>some gender</gender>
</user>
</recipients>
</note>

 

Would like this to return a multidimensional array follow same structure such as...

 

[Note] =>array([id] => a number, etc.. [sender] => array([username] => someuser, etc... [profile_photo_urls] => array([tiny_photo_url] => a url, etc..))),

 

[Note] =>array([id] => a number, etc.. [sender] => array([username] => someuser, etc... [profile_photo_urls] => array([tiny_photo_url] => a url, etc..)))

 

Hope this is clear. Like I said, little XML experience so there could be something blatantly obvious that I've never seen. I just don't wanna have to write a verbose datamining script if I don't have to...

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/75488-solved-xml-to-array/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.