Converts an XML file into an array

Source: Internet
Author: User
<?php/* * To change this template, choose Tools | Templates * and open the template in the editor. */class Xmltoarray {function __construct () {} function Xml2array ($contents, $get _attributes=1, $priorit        y = ' tag ') {if (! $contents) return array ();            if (!function_exists (' xml_parser_create ')) {//print "' xml_parser_create () ' function not found! ';        return Array (); }//get the XML parser of php-php must has this module for the parser to work $parser = Xml_parser_create        (''); Xml_parser_set_option ($parser, xml_option_target_encoding, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/ Php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss xml_parser_set_option ($parser, XML_OPTION_        case_folding, 0);        Xml_parser_set_option ($parser, Xml_option_skip_white, 1);        Xml_parse_into_struct ($parser, Trim ($contents), $xml _values);        Xml_parser_free ($parser); If(! $xml _values) return;            Hmm ...        initializations $xml _array = Array ();        $parents = Array ();        $opened _tags = Array ();        $arr = Array (); $current = & $xml _array;        Refference//go through the tags. $repeated _tag_index = Array (); Multiple tags with same name would be turned to an array foreach ($xml _values as $data) {unset ($attr Ibutes, $value);  Remove existing values, or there'll be trouble//this command would extract these variables into the foreach            Scope//Tag (string), type (string), level (int), attributes (array). Extract ($data);            We could use the array by itself, but this cooler.            $result = Array ();            $attributes _data = Array ();                if (Isset ($value)) {if ($priority = = ' Tag ') $result = $value; else $result [' value '] = $value; Put the value in a assoc array if weis in the ' Attribute ' mode}//set the attributes too.                    if (Isset ($attributes) and $get _attributes) {foreach ($attributes as $attr = = $val) {                    if ($priority = = ' tag ') $attributes _data[$attr] = $val; else $result [' attr '] [$attr] = $val; Set all the attributes in an array called ' attr '}}//see tag status and do the Nee            Ded.                if ($type = = "Open") {//the starting of the tag ' <tag> ' $parent [$level-1] = & $current; if (!is_array ($current) or (!in_array ($tag, Array_keys ($current))) {//insert New tag $current                    [$tag] = $result; if ($attributes _data) $current [$tag.                    ' _attr '] = $attributes _data; $repeated _tag_index[$tag. '_' .                    $level] = 1;       $current = & $current [$tag];         } else {//there is another element with the same tag name if (Isset ($current [$tag][0])) {//i F there is a 0th element it was already an array $current [$tag] [$repeated _tag_index[$tag. '_' .                        $level]] = $result; $repeated _tag_index[$tag. '_' .                    $level]++;                        } else {//this section would make the value a array if multiple tags with the same name appear together $current [$tag] = Array ($current [$tag], $result); This would combine the existing item and the new item together to make an array $repeated _tag_inde x[$tag. '_' .                        $level] = 2; if (Isset ($current [$tag. ' _attr ']) {//the attribute of the last (0th) tag must is moved as well $current [$tag] [' 0_attr ' ] = $current [$tag.                            ' _attr ']; Unset ($current [$tag.                        ' _attr ']); }                    }                    $Last_item_index = $repeated _tag_index[$tag. '_' .                    $level]-1;                $current = & $current [$tag] [$last _item_index]; }} elseif ($type = = "complete") {//tags, ends in 1 line ' <tag/> '//see if the key I                s already taken.                    if (!isset ($current [$tag])) {//new Key $current [$tag] = $result; $repeated _tag_index[$tag. '_' .                    $level] = 1; if ($priority = = ' Tag ' and $attributes _data) $current [$tag.                ' _attr '] = $attributes _data; } else {//if taken, put all things inside a list (array) If (Isset ($current [$tag][0]) and Is_array ($cur                        rent[$tag]) {//if It is already an array ...//. Push the new element into the-that array. $current [$tag] [$repeated _tag_index[$tag. '_' .                        $level]] = $result; if ($priority = = ' Tag ' and $get _attributes and $attriButes_data) {$current [$tag] [$repeated _tag_index[$tag. '_' . $level].                        ' _attr '] = $attributes _data; } $repeated _tag_index[$tag. '_' .                    $level]++; } else {//if it is isn't an array ... $current [$tag] = Array ($current [$tag], $result);//... Make it an array using using the existing value and the new value $repeated _tag_index[$tag. '_' .                        $level] = 1; if ($priority = = ' Tag ' and $get _attributes) {if (Isset ($current [$tag. ' _attr ']) {//the attribute of the last (0th) tag must is moved as well $current [$tag] [' 0_a TTR '] = $current [$tag.                                ' _attr ']; Unset ($current [$tag.                            ' _attr ']); if ($attributes _data) {$current [$tag] [$repeated _tag_index[$ta G. '_' . $level]. '_attr '] = $attributes _data; }} $repeated _tag_index[$tag. '_' . $level]++; 0 and 1 index is already taken}}} elseif ($type = = ' Close ') {//end of T            Ag ' </tag> ' $current = & $parent [$level-1];    }} return $xml _array; }}?>


<?php/**&nbsp;* the returned multidimensional array into a one-dimensional array using recursion &nbsp;*/    function datatosession ($array) {        foreach ($array as $ Key = $value) {            if (Is_array ($value) && $value! = null) {                $this->datatosession ($value);            } else {                if (is_string ($value)) {                    $_session["$key"] = $value;                }}}    } ; >
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.