当前位置:Gxlcms > PHP教程 > 将xml文件转换成以为数组

将xml文件转换成以为数组

时间:2021-07-01 10:21:17 帮助过:12人阅读

  1. <?php
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. class XmlToArray {
  7. function __construct() {
  8. }
  9. function xml2array($contents, $get_attributes=1, $priority = 'tag') {
  10. if (!$contents)
  11. 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"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
  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)
  24. return; //Hmm...
  25. //Initializations
  26. $xml_array = array();
  27. $parents = array();
  28. $opened_tags = array();
  29. $arr = array();
  30. $current = &$xml_array; //Refference
  31. //Go through the tags.
  32. $repeated_tag_index = array(); //Multiple tags with same name will be turned into an array
  33. foreach ($xml_values as $data) {
  34. unset($attributes, $value); //Remove existing values, or there will be trouble
  35. //This command will extract these variables into the foreach scope
  36. // tag(string), type(string), level(int), attributes(array).
  37. extract($data); //We could use the array by itself, but this cooler.
  38. $result = array();
  39. $attributes_data = array();
  40. if (isset($value)) {
  41. if ($priority == 'tag')
  42. $result = $value;
  43. else
  44. $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
  45. }
  46. //Set the attributes too.
  47. if (isset($attributes) and $get_attributes) {
  48. foreach ($attributes as $attr => $val) {
  49. if ($priority == 'tag')
  50. $attributes_data[$attr] = $val;
  51. else
  52. $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  53. }
  54. }
  55. //See tag status and do the needed.
  56. if ($type == "open") {//The starting of the tag '<tag>'
  57. $parent[$level - 1] = &$current;
  58. if (!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
  59. $current[$tag] = $result;
  60. if ($attributes_data)
  61. $current[$tag . '_attr'] = $attributes_data;
  62. $repeated_tag_index[$tag . '_' . $level] = 1;
  63. $current = &$current[$tag];
  64. } else { //There was another element with the same tag name
  65. if (isset($current[$tag][0])) {//If there is a 0th element it is already an array
  66. $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  67. $repeated_tag_index[$tag . '_' . $level]++;
  68. } else {//This section will make the value an array if multiple tags with the same name appear together
  69. $current[$tag] = array($current[$tag], $result); //This will combine the existing item and the new item together to make an array
  70. $repeated_tag_index[$tag . '_' . $level] = 2;
  71. if (isset($current[$tag . '_attr'])) { //The attribute of the last(0th) tag must be moved as well
  72. $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  73. unset($current[$tag . '_attr']);
  74. }
  75. }
  76. $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
  77. $current = &$current[$tag][$last_item_index];
  78. }
  79. } elseif ($type == "complete") { //Tags that ends in 1 line '<tag />'
  80. //See if the key is already taken.
  81. if (!isset($current[$tag])) { //New Key
  82. $current[$tag] = $result;
  83. $repeated_tag_index[$tag . '_' . $level] = 1;
  84. if ($priority == 'tag' and $attributes_data)
  85. $current[$tag . '_attr'] = $attributes_data;
  86. } else { //If taken, put all things inside a list(array)
  87. if (isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
  88. // ...push the new element into that array.
  89. $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  90. if ($priority == 'tag' and $get_attributes and $attributes_data) {
  91. $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  92. }
  93. $repeated_tag_index[$tag . '_' . $level]++;
  94. } else { //If it is not an array...
  95. $current[$tag] = array($current[$tag], $result); //...Make it an array using using the existing value and the new value
  96. $repeated_tag_index[$tag . '_' . $level] = 1;
  97. if ($priority == 'tag' and $get_attributes) {
  98. if (isset($current[$tag . '_attr'])) { //The attribute of the last(0th) tag must be moved as well
  99. $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  100. unset($current[$tag . '_attr']);
  101. }
  102. if ($attributes_data) {
  103. $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  104. }
  105. }
  106. $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
  107. }
  108. }
  109. } elseif ($type == 'close') { //End of tag '</tag>'
  110. $current = &$parent[$level - 1];
  111. }
  112. }
  113. return $xml_array;
  114. }
  115. }
  116. ?>


  1. <?php
  2. /**
  3. &nbsp;*用递归法将返回的多维数组存进一维数组
  4. &nbsp;*/
  5. function datatosession($array) {
  6. foreach ($array as $key => $value) {
  7. if (is_array($value) && $value != null) {
  8. $this->datatosession($value);
  9. } else {
  10. if (is_string($value)) {
  11. $_SESSION["$key"] = $value;
  12. }
  13. }
  14. }
  15. }
  16. &nbsp;
  17. ?>

人气教程排行