一个将xml转数组的php函数

php教程评论647 views阅读模式
  1. /**

  2. * xml2array() will convert the given XML text to an array in the XML structure.
  3. * Arguments : $contents - The XML text
  4. * $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.
  5. * $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.
  6. * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.
  7. * Examples: $array = xml2array(file_get_contents('feed.xml'));
  8. * $array = xml2array(file_get_contents('feed.xml', 1, 'attribute'));
  9. */
  10. function xml2array($contents, $get_attributes=1, $priority = 'tag') {
  11. if(!$contents) return array();
  12. if(!function_exists('xml_parser_create')) {
  13. //print "'xml_parser_create()' function not found!";
  14. return array();
  15. }
  16. //Get the XML parser of PHP - PHP must have this module for the parser to work
  17. $parser = xml_parser_create('');
  18. xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
  19. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  20. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  21. xml_parse_into_struct($parser, trim($contents), $xml_values);
  22. xml_parser_free($parser);
  23. if(!$xml_values) return;//Hmm...
  24. //Initializations bbs.it-home.org
  25. $xml_array = array();
  26. $parents = array();
  27. $opened_tags = array();
  28. $arr = array();
  29. $current = &$xml_array; //Refference
  30. //Go through the tags.
  31. $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
  32. foreach($xml_values as $data) {
  33. unset($attributes,$value);//Remove existing values, or there will be trouble
  34. //This command will extract these variables into the foreach scope
  35. // tag(string), type(string), level(int), attributes(array).
  36. extract($data);//We could use the array by itself, but this cooler.
  37. $result = array();
  38. $attributes_data = array();

  39. if(isset($value)) {

  40. if($priority == 'tag') $result = $value;
  41. else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
  42. }
  43. //Set the attributes too.
  44. if(isset($attributes) and $get_attributes) {
  45. foreach($attributes as $attr => $val) {
  46. if($priority == 'tag') $attributes_data[$attr] = $val;
  47. else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  48. }
  49. }
  50. //See tag status and do the needed.
  51. if($type == "open") {//The starting of the tag ' '
  52. $parent[$level-1] = &$current;
  53. if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
  54. $current[$tag] = $result;
  55. if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
  56. $repeated_tag_index[$tag.'_'.$level] = 1;
  57. $current = &$current[$tag];
  58. } else { //There was another element with the same tag name
  59. if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
  60. $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  61. $repeated_tag_index[$tag.'_'.$level]++;
  62. } else {//This section will make the value an array if multiple tags with the same name appear together
  63. $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
  64. $repeated_tag_index[$tag.'_'.$level] = 2;

  65. if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well

  66. $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  67. unset($current[$tag.'_attr']);
  68. }
  69. }
  70. $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
  71. $current = &$current[$tag][$last_item_index];
  72. }
  73. } elseif($type == "complete") { //Tags that ends in 1 line ' '
  74. //See if the key is already taken.
  75. if(!isset($current[$tag])) { //New Key
  76. $current[$tag] = $result;
  77. $repeated_tag_index[$tag.'_'.$level] = 1;
  78. if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
  79. } else { //If taken, put all things inside a list(array)
  80. if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
  81. // ...push the new element into that array.
  82. $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;

  83. if($priority == 'tag' and $get_attributes and $attributes_data) {

  84. $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  85. }
  86. $repeated_tag_index[$tag.'_'.$level]++;
  87. } else { //If it is not an array...
  88. $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
  89. $repeated_tag_index[$tag.'_'.$level] = 1;
  90. if($priority == 'tag' and $get_attributes) {
  91. if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well

  92. $current[$tag]['0_attr'] = $current[$tag.'_attr'];

  93. unset($current[$tag.'_attr']);
  94. }

  95. if($attributes_data) {

  96. $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  97. }
  98. }
  99. $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
  100. }
  101. }
  102. } elseif($type == 'close') { //End of tag ''
  103. $current = &$parent[$level-1];
  104. }
  105. }

复制代码

调用示例:

复制代码

企鹅博客
  • 本文由 发表于 2019年9月16日 22:42:20
  • 转载请务必保留本文链接:https://www.qieseo.com/317883.html

发表评论