PHP XML to convert the code to an array

Source: Internet
Author: User
  1. XML-to-array, including the root key, ignoring empty elements and attributes, with significant 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]); Recursive
  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. Xml to array, not including 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. An XPATH-like array selector

  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 was not exists: '. $curpath);
  59. }
  60. Return $self ($result, $next);
  61. }

  62. If the input array is a full numeric key, the element values are transferred sequentially to the $callback, otherwise they are transferred 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 shuld is an array! ');
  67. if (!is_callable ($callback)) die (' Parameter 2 shuld is 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. ?>

Copy Code
  • Related Article

    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.