PHP XML goto Array function _php Tutorial

Source: Internet
Author: User
Tags xml parser
/*** Xml2array () would convert the given XML text to a array in the XML structure. * link:http://www.bin-co.com/php/scripts/xml2array/* Arguments: $contents-the XML text * $get _attribu Tes-1 or 0. If This is 1 the function would get the attributes as well as the tag values-this results in a different array structure In the return value. * $priority-can be ' tag ' or ' attribute '. This would change the resulting array sturcture. For ' tag ', the tags is given more importance. * Return:the parsed XML in an array form. Use Print_r () to see the resulting array structure. * Examples: $array = Xml2array (file_get_contents (' feed.xml ')); * $array = Xml2array (file_get_contents (' Feed.xml ', 1, ' attribute '));     */function Xml2array ($contents, $get _attributes=1, $priority = ' 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 ($attributes, $value);//remove existing values, or there'll be trouble//this command would extract these variables into the foreach scope//tags (string), type (string), level (int), Attri         Butes (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 we is 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 needed. if ($type = = "Open") {//the starting of the tag '
 
  
' $parent [$level-1] = & $current; if (!is_array ($current) or (!in_array ($tag, Array_keys ($current))) {//insert New tag $current [$tag] = $re                  Sult; if ($attributes _data) $current [$tag.                 ' _attr '] = $attributes _data; $repeated _tag_index[$tag. '                _ '. $level] = 1;             $current = & $current [$tag]; } else {//there was another element with the same tag name if (Isset ($current [$tag][0])) {//if There is a 0 th element It is 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'll combine the existing item and the new item together to make a N Array $repeated _tag_index[$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 '
  
    
  '//see if the key is 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 ($current [$tag]))                     {//if It is already an array ...//... push the new element into the. Array. $current [$tag] [$repeated _tag_index[$tag. '                                            _ '. $level]] = $result; if ($priority = = ' Tag ' and $get _attributes and $attributes _data) {$current [$tag] [$repeated _tag_ind ex[$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_attr '] = $current [$tag. '                              _attr ']; Unset ($current [$tag. '                          _attr ']); } if ($attributes _data) {$current [$tag] [$re peated_tag_index[$tag. ' _ '. $level].                          ' _attr '] = $attributes _data; }} $repeated _tag_index[$tag. ' _ '. $level]++; 0 and 1 index is already taken}}} elseif ($type = = ' Close ') {//end of tag '
 ' $current = & $parent [$level-1]; }} return ($xml _array); }?> function Description and example $arr = Xml2array (file_get_contents ("Tools.xml"), 1, ' attribute '); query keyword XML array

http://www.bkjia.com/PHPjc/447001.html www.bkjia.com true http://www.bkjia.com/PHPjc/447001.html techarticle /*** Xml2array () would convert the given XML text to a array in the XML structure. * LINK:HTTP://WWW.BIN-CO.COM/PHP/S cripts/xml2array/* Arguments: $contents-the XML text * $ge ...

  • Related Article

    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.