vbnullchar Posted July 11, 2007 Share Posted July 11, 2007 i need to convert this xml to an array <?xml version="1.0"?> <addressBook> <contact> <guid>739537250f3146d1968372f982d0fa71</guid> <email>aborja@abc.com.ph</email> <fullName>aaaaaaa</fullName> </contact> <contact> <guid>c62a7f3e5d8b48a68cbf0d26999c27f4</guid> <email>agomez@abc.com.ph</email> <fullName>bbbbb</fullName> </contact> </addressBook> something like this addressbook=array( contact[0]=array('fullName'=>'aaaaaa', 'guid'=>'739537250f3146d1968372f982d0fa71','email'=>'aborja@abc.com.ph' ), contact[1]=array('fullName'=>'bbbbbb', 'guid'=>'739537250f3146d1968372f982d0fa71','email'=>'aborja@abc.com.ph' ), ); Quote Link to comment Share on other sites More sharing options...
per1os Posted July 11, 2007 Share Posted July 11, 2007 www.php.net/simplexml To get it into that type of a form above it would require your own algorithm. Instead of doing that since you are using PHP5, I suggest looking into simplexml. It may solve your problem. Quote Link to comment Share on other sites More sharing options...
vbnullchar Posted July 11, 2007 Author Share Posted July 11, 2007 thanks it makes work alot easier Quote Link to comment Share on other sites More sharing options...
vbnullchar Posted July 16, 2007 Author Share Posted July 16, 2007 how to sort that by email? Quote Link to comment Share on other sites More sharing options...
keeB Posted July 16, 2007 Share Posted July 16, 2007 Already have this done <?php class xml { private $url; public $data; private $vals; private $debug = false; public function __construct($url) { $this->url = $url; $xml = file_get_contents($url); $data = $this->xml2array($xml); $this->data = $data; if ($this->debug) { print "<pre>"; print_r($data); } } private function &last(&$array){ if (!count($array)) return null; end($array); return $array[key($array)]; } private function parsexml(&$vals, &$dom, &$lev){ do { $curr = current($vals); $lev = $curr['level']; $tag = strtolower($curr['tag']); switch ($curr['type']) { case 'open': if (!isset($dom[$tag])) $dom[$tag] = array(); array_push($dom[$tag], array()); $new =& $this->last($dom[$tag]); next($vals); $this->parsexml(&$vals, $new, $lev); break; case 'cdata': break; case 'complete': if (!isset($dom[$tag])) $dom[$tag] = $curr['value']; else array_push($dom[$tag] , $curr['value']); break; case 'close': return; } } while (next($vals)!==FALSE); } private function xml2array($xml){ $xml_parser = xml_parser_create(); xml_parse_into_struct($xml_parser, $xml, $vals); xml_parser_free($xml_parser); reset($vals); $dom = array(); $lev = 0; $this->parsexml($vals, $dom, $lev); return $dom; } } ?> Good luck Quote Link to comment Share on other sites More sharing options...
vbnullchar Posted July 16, 2007 Author Share Posted July 16, 2007 havent tried your code yet but does it sort array values? Quote Link to comment Share on other sites More sharing options...
keeB Posted July 16, 2007 Share Posted July 16, 2007 ? Quote Link to comment 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.