A php function that converts xml into arrays.

Source: Internet
Author: User
A php function that converts xml into arrays.

  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 cocould 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 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. }

  106. Return ($ xml_array );

  107. }
  108. ?>

Call example:

  1. // Php XML to array function call
  2. $ Arr = xml2array (file_get_contents ("tools. xml"), 1, 'attribute ');
  3. ?>

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.