note 109032 modified in function.xml-parse-into-struct by danbrown

From: Date: Thu, 14 Jun 2012 15:24:04 +0000
Subject: note 109032 modified in function.xml-parse-into-struct by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188607@lists.php.net to get a copy of this message
To get out of an XML-Structure like this:&#13;&#10;&#60;root&#62;&#60;element&#62;&#60;subelement attribute=&#34;value&#34;&#62;some text&#60;/subelement&#62;&#60;/element&#62;&#60;anotherelement&#62;Hello World&#60;/anotherelement&#62;&#60;/root&#62;&#13;&#10;something like this:&#13;&#10;&#13;&#10;Array&#13;&#10;(&#13;&#10; [tag] =&#62; ROOT&#13;&#10; [0] =&#62; Array&#13;&#10; (&#13;&#10; [tag] =&#62; ELEMENT&#13;&#10; [0] =&#62; Array&#13;&#10; (&#13;&#10; [tag] =&#62; SUBELEMENT&#13;&#10; [attributes] =&#62; Array&#13;&#10; (&#13;&#10; [ATTRIBUTE] =&#62; value&#13;&#10; )&#13;&#10;&#13;&#10; [value] =&#62; some text&#13;&#10; )&#13;&#10;&#13;&#10; )&#13;&#10;&#13;&#10; [1] =&#62; Array&#13;&#10; (&#13;&#10; [tag] =&#62; ANOTHERELEMENT&#13;&#10; [value] =&#62; Hello World&#13;&#10; )&#13;&#10;&#13;&#10;)&#13;&#10;&#13;&#10;I have written a short and pretty simple function:&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;function xml2array($xml){&#13;&#10;&#9;$opened = array();&#13;&#10;&#9;$opened[1] = 0;&#13;&#10;&#9;$xml_parser = xml_parser_create();&#13;&#10;&#9;xml_parse_into_struct($xml_parser, $xml, $xmlarray);&#13;&#10;&#9;$array = array_shift($xmlarray);&#13;&#10;&#9;unset($array[&#34;level&#34;]);&#13;&#10;&#9;unset($array[&#34;type&#34;]);&#13;&#10;&#9;$arrsize = sizeof($xmlarray);&#13;&#10;&#9;for($j=0;$j&#60;$arrsize;$j++){&#13;&#10;&#9;&#9;$val = $xmlarray[$j];&#13;&#10;&#9;&#9;switch($val[&#34;type&#34;]){&#13;&#10;&#9;&#9;&#9;case &#34;open&#34;:&#13;&#10;&#9;&#9;&#9;&#9;$opened[$val[&#34;level&#34;]]=0;&#13;&#10;&#9;&#9;&#9;case &#34;complete&#34;:&#13;&#10;&#9;&#9;&#9;&#9;$index = &#34;&#34;;&#13;&#10;&#9;&#9;&#9;&#9;for($i = 1; $i &#60; ($val[&#34;level&#34;]); $i++)&#13;&#10;&#9;&#9;&#9;&#9;&#9;$index .= &#34;[&#34; . $opened[$i] . &#34;]&#34;;&#13;&#10;&#9;&#9;&#9;&#9;$path = explode(&#39;][&#39;, substr($index, 1, -1));&#13;&#10;&#9;&#9;&#9;&#9;$value = &#38;$array;&#13;&#10;&#9;&#9;&#9;&#9;foreach($path as $segment)&#13;&#10;&#9;&#9;&#9;&#9;&#9;$value = &#38;$value[$segment];&#13;&#10;&#9;&#9;&#9;&#9;$value = $val;&#13;&#10;&#9;&#9;&#9;&#9;unset($value[&#34;level&#34;]);&#13;&#10;&#9;&#9;&#9;&#9;unset($value[&#34;type&#34;]);&#13;&#10;&#9;&#9;&#9;&#9;if($val[&#34;type&#34;] == &#34;complete&#34;)&#13;&#10;&#9;&#9;&#9;&#9;&#9;$opened[$val[&#34;level&#34;]-1]++;&#13;&#10;&#9;&#9;&#9;break;&#13;&#10;&#9;&#9;&#9;case &#34;close&#34;:&#13;&#10;&#9;&#9;&#9;&#9;$opened[$val[&#34;level&#34;]-1]++;&#13;&#10;&#9;&#9;&#9;&#9;unset($opened[$val[&#34;level&#34;]]);&#13;&#10;&#9;&#9;&#9;break;&#13;&#10;&#9;&#9;}&#13;&#10;&#9;}&#13;&#10;&#9;return $array;&#13;&#10;}&#13;&#10;?&#62; --was-- To get out of an XML-Structure like this: <root><element><subelement attribute="value">some text</subelement></element><anotherelement>Hello World</anotherelement></root> something like this: Array ( [tag] => ROOT [0] => Array ( [tag] => ELEMENT [0] => Array ( [tag] => SUBELEMENT [attributes] => Array ( [ATTRIBUTE] => value ) [value] => some text ) ) [1] => Array ( [tag] => ANOTHERELEMENT [value] => Hello World ) ) I have written a short and pretty simple function: function xml2array($xml){ $opened = array(); $opened[1] = 0; $xml_parser = xml_parser_create(); xml_parse_into_struct($xml_parser, $xml, $xmlarray); $array = array_shift($xmlarray); unset($array["level"]); unset($array["type"]); $arrsize = sizeof($xmlarray); for($j=0;$j<$arrsize;$j++){ $val = $xmlarray[$j]; switch($val["type"]){ case "open": $opened[$val["level"]]=0; case "complete": $index = ""; for($i = 1; $i < ($val["level"]); $i++) $index .= "[" . $opened[$i] . "]"; $path = explode('][', substr($index, 1, -1)); $value = &$array; foreach($path as $segment) $value = &$value[$segment]; $value = $val; unset($value["level"]); unset($value["type"]); if($val["type"] == "complete") $opened[$val["level"]-1]++; break; case "close": $opened[$val["level"]-1]++; unset($opened[$val["level"]]); break; } } return $array; } http://php.net/manual/en/function.xml-parse-into-struct.php

« previous php.notes (#188607) next »