Code for converting php xml to an array

Source: Internet
Author: User
Code for converting php xml to an array

  1. // Xml to array, including the root key, ignore empty elements and attributes, there are still major errors

  2. Function xml_to_array ($ xml)
  3. {
  4. $ Reg = "/<(\ w +) [^>] *?> ([\ X00-\ xFF] *?) <\/\ 1> /";
  5. If (preg_match_all ($ reg, $ xml, $ matches ))
  6. {
  7. $ Count = count ($ matches [0]);
  8. $ Arr = array ();
  9. For ($ I = 0; $ I <$ count; $ I ++)
  10. {
  11. $ Key = $ matches [1] [$ I];
  12. $ Val = xml_to_array ($ matches [2] [$ I]); // recursion
  13. If (array_key_exists ($ key, $ arr ))
  14. {
  15. If (is_array ($ arr [$ key])
  16. {
  17. If (! Array_key_exists (0, $ arr [$ key])
  18. {
  19. $ Arr [$ key] = array ($ arr [$ key]);
  20. }
  21. } Else {
  22. $ Arr [$ key] = array ($ arr [$ key]);
  23. }
  24. $ Arr [$ key] [] = $ val;
  25. } Else {
  26. $ Arr [$ key] = $ val;
  27. }
  28. }
  29. Return $ arr;
  30. } Else {
  31. Return $ xml;
  32. }
  33. }

  34. // Convert Xml to an array, excluding the root key

  35. Function xmltoarray ($ xml)
  36. {
  37. $ Arr = xml_to_array ($ xml );
  38. $ Key = array_keys ($ arr );
  39. Return $ arr [$ key [0];
  40. }

  41. // Array selector similar to XPATH

  42. Function xml_array_select ($ arr, $ arrpath)
  43. {
  44. $ Arrpath = trim ($ arrpath ,'/');
  45. If (! $ Arrpath) return $ arr;
  46. $ Self = 'XML _ array_select ';
  47. $ Pos = strpos ($ arrpath ,'/');
  48. $ Pos = $ pos? $ Pos: strlen ($ arrpath );
  49. $ Curpath = substr ($ arrpath, 0, $ pos );
  50. $ Next = substr ($ arrpath, $ pos );
  51. If (preg_match ("/\ [(\ d +) \] $/", $ curpath, $ predicate ))
  52. {
  53. $ Curpath = substr ($ curpath, 0, strpos ($ curpath, "[{$ predicate [1]}]");
  54. $ Result = $ arr [$ curpath] [$ predicate [1];
  55. } Else $ result = $ arr [$ curpath];
  56. If (is_array ($ arr )&&! Array_key_exists ($ curpath, $ arr ))
  57. {
  58. Die ('Key is not exists: '. $ curpath );
  59. }
  60. Return $ self ($ result, $ next );
  61. }

  62. // If the input array is a full-number key, the element values are transmitted to $ callback in sequence; otherwise, the elements are transmitted to $ callback.

  63. Function xml_array_each ($ arr, $ callback)
  64. {
  65. If (func_num_args () <2) die ('parameters error ');
  66. If (! Is_array ($ arr) die ('parameter 1 shcould be an array! ');
  67. If (! Is_callable ($ callback) die ('Parameter 2 shcould be an function! ');
  68. $ Keys = array_keys ($ arr );
  69. $ Isok = true;
  70. Foreach ($ keys as $ key) {if (! Is_int ($ key) {$ isok = false; break ;}}
  71. If ($ isok)
  72. Foreach ($ arr as $ val) $ result [] = $ callback ($ val );
  73. Else
  74. $ Result [] = $ callback ($ arr );
  75. Return $ result;
  76. }
  77. ?>

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.