note 51310 added to function.xml-parse-into-struct

From: Date: Sat, 26 Mar 2005 23:03:17 +0000
Subject: note 51310 added to function.xml-parse-into-struct
Groups: php.notes 
Request: Send a blank email to php-notes+get-87036@lists.php.net to get a copy of this message
XMLtoArray created by dudus at onet dot pl was not good enough for me, that's why i decided to create my own version, and here's the result: <?php function &last(&$array) { if (!count($array)) return null; end($array); return $array[key($array)]; } function myParseXML(&$vals, &$dom, &$lev) { do { $curr = current($vals); $lev = $curr['level']; switch ($curr['type']) { case 'open': if (isset($dom[$curr['tag']])) { $tmp = $dom[$curr['tag']]; if (!$tmp['__multi']) $dom[$curr['tag']] = array('__multi' => true, $tmp); array_push($dom[$curr['tag']], array()); $new =& last($dom[$curr['tag']]); } else { $dom[$curr['tag']] = array(); $new =& $dom[$curr['tag']]; } next($vals); myParseXML(&$vals, $new, $lev); break; case 'cdata': break; case 'complete': if (!isset($dom[$curr['tag']])) $dom[$curr['tag']] = $curr['value']; else { if (is_array($dom[$curr['tag']])) array_push($dom[$curr['tag']] , $curr['value']); else array_push($dom[$curr['tag']] = array($dom[$curr['tag']]) , $curr['value']); } break; case 'close': return; } } while (next($vals)!==FALSE); } function MyXMLtoArray($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; myParseXML($vals, $dom, $lev); return $dom; } ?> MyXMLtoArray() is that function you should call, other are for "internal" use. P.S. It's 4-5 time faster then dudus at onet dot pl 's code ---- Manual Page -- http://www.php.net/manual/en/function.xml-parse-into-struct.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+51310 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51310&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51310&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51310&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51310&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51310&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+51310&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#87036) next »