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

From: Date: Tue, 14 Jun 2005 23:09:59 +0000
Subject: note 53840 added to function.xml-parse-into-struct
Groups: php.notes 
Request: Send a blank email to php-notes+get-90434@lists.php.net to get a copy of this message
Some of the code posted previously seemed absurdly complex, so I thought I'd see if I could simplify things a bit. Here's what I came up with. (I'm still a PHP newbie, so perhaps I simplified too much. ;-) Untested for speed, but it *should* be pretty quick since it uses references instead of lookups or (gack) evals. Enjoy! /* * Convert a string containing XML into a nested array * * Takes a raw XML string as input * Returns a nested array */ function xml2array ($xml_data) { // parse the XML datastring $xml_parser = xml_parser_create (); xml_parse_into_struct ($xml_parser, $xml_data, $vals, $index); xml_parser_free ($xml_parser); // convert the parsed data into a PHP datatype $params = array(); $ptrs[0] = & $params; foreach ($vals as $xml_elem) { $level = $xml_elem['level'] - 1; switch ($xml_elem['type']) { case 'open': $tag_or_id = (array_key_exists ('attributes', $xml_elem)) ? $xml_elem['attributes']['ID'] : $xml_elem['tag']; $ptrs[$level][$tag_or_id] = array (); $ptrs[$level+1] = & $ptrs[$level][$tag_or_id]; break; case 'complete': $ptrs[$level][$xml_elem['tag']] = (isset ($xml_elem['value'])) ? $xml_elem['value'] : ''; break; } } return ($params); } ---- 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+53840 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53840&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53840&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53840&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53840&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53840&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+53840&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#90434) next »