PHP XML To Array


SUBMITTED BY: Guest

DATE: July 12, 2013, 8:30 a.m.

FORMAT: PHP

SIZE: 5.4 kB

HITS: 1072

  1. function xml2array($url, $get_attributes = 1, $priority = 'tag')
  2. {
  3. $contents = "";
  4. if (!function_exists('xml_parser_create'))
  5. {
  6. return array ();
  7. }
  8. $parser = xml_parser_create('');
  9. if (!($fp = @ fopen($url, 'rb')))
  10. {
  11. return array ();
  12. }
  13. while (!feof($fp))
  14. {
  15. $contents .= fread($fp, 8192);
  16. }
  17. fclose($fp);
  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)
  24. return; //Hmm...
  25. $xml_array = array ();
  26. $parents = array ();
  27. $opened_tags = array ();
  28. $arr = array ();
  29. $current = & $xml_array;
  30. $repeated_tag_index = array ();
  31. foreach ($xml_values as $data)
  32. {
  33. unset ($attributes, $value);
  34. extract($data);
  35. $result = array ();
  36. $attributes_data = array ();
  37. if (isset ($value))
  38. {
  39. if ($priority == 'tag')
  40. $result = $value;
  41. else
  42. $result['value'] = $value;
  43. }
  44. if (isset ($attributes) and $get_attributes)
  45. {
  46. foreach ($attributes as $attr => $val)
  47. {
  48. if ($priority == 'tag')
  49. $attributes_data[$attr] = $val;
  50. else
  51. $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  52. }
  53. }
  54. if ($type == "open")
  55. {
  56. $parent[$level -1] = & $current;
  57. if (!is_array($current) or (!in_array($tag, array_keys($current))))
  58. {
  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. }
  65. else
  66. {
  67. if (isset ($current[$tag][0]))
  68. {
  69. $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  70. $repeated_tag_index[$tag . '_' . $level]++;
  71. }
  72. else
  73. {
  74. $current[$tag] = array (
  75. $current[$tag],
  76. $result
  77. );
  78. $repeated_tag_index[$tag . '_' . $level] = 2;
  79. if (isset ($current[$tag . '_attr']))
  80. {
  81. $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  82. unset ($current[$tag . '_attr']);
  83. }
  84. }
  85. $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
  86. $current = & $current[$tag][$last_item_index];
  87. }
  88. }
  89. elseif ($type == "complete")
  90. {
  91. if (!isset ($current[$tag]))
  92. {
  93. $current[$tag] = $result;
  94. $repeated_tag_index[$tag . '_' . $level] = 1;
  95. if ($priority == 'tag' and $attributes_data)
  96. $current[$tag . '_attr'] = $attributes_data;
  97. }
  98. else
  99. {
  100. if (isset ($current[$tag][0]) and is_array($current[$tag]))
  101. {
  102. $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  103. if ($priority == 'tag' and $get_attributes and $attributes_data)
  104. {
  105. $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  106. }
  107. $repeated_tag_index[$tag . '_' . $level]++;
  108. }
  109. else
  110. {
  111. $current[$tag] = array (
  112. $current[$tag],
  113. $result
  114. );
  115. $repeated_tag_index[$tag . '_' . $level] = 1;
  116. if ($priority == 'tag' and $get_attributes)
  117. {
  118. if (isset ($current[$tag . '_attr']))
  119. {
  120. $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  121. unset ($current[$tag . '_attr']);
  122. }
  123. if ($attributes_data)
  124. {
  125. $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  126. }
  127. }
  128. $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
  129. }
  130. }
  131. }
  132. elseif ($type == 'close')
  133. {
  134. $current = & $parent[$level -1];
  135. }
  136. }
  137. return ($xml_array);
  138. }

comments powered by Disqus