benjaminbeazy Posted October 31, 2007 Share Posted October 31, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/75488-solved-xml-to-array/ Share on other sites More sharing options...
bache Posted October 31, 2007 Share Posted October 31, 2007 Try this: http://bg.php.net/manual/en/ref.simplexml.php - it is very easy to use. Quote Link to comment https://forums.phpfreaks.com/topic/75488-solved-xml-to-array/#findComment-381846 Share on other sites More sharing options...
benjaminbeazy Posted October 31, 2007 Author Share Posted October 31, 2007 Cool, thank you. I think this will work. Quote Link to comment https://forums.phpfreaks.com/topic/75488-solved-xml-to-array/#findComment-381850 Share on other sites More sharing options...
benjaminbeazy Posted October 31, 2007 Author Share Posted October 31, 2007 Hermmm, okay. I'm doing this on a work server where we apparently have php ver. 4.3.2 so i can't use simplexml anything else I could try? Quote Link to comment https://forums.phpfreaks.com/topic/75488-solved-xml-to-array/#findComment-381860 Share on other sites More sharing options...
bache Posted October 31, 2007 Share Posted October 31, 2007 May be this http://www.phpfreaks.com/script/view/202.php but I haven't tried it. Quote Link to comment https://forums.phpfreaks.com/topic/75488-solved-xml-to-array/#findComment-381864 Share on other sites More sharing options...
benjaminbeazy Posted October 31, 2007 Author Share Posted October 31, 2007 awesome! works great! any chance at getting something similar for json? Quote Link to comment https://forums.phpfreaks.com/topic/75488-solved-xml-to-array/#findComment-381872 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.