Home > Backend Development > PHP Tutorial > php XML to array code conversion

php XML to array code conversion

WBOY
Release: 2016-07-25 09:04:13
Original
750 people have browsed it
  1. // Convert Xml to array, including root keys, ignoring 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. // Xml to array, excluding 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. // 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 is not exists:' . $curpath );
  59. }
  60. return $self($result, $next);
  61. }< /p>
  62. // If the input array is an all-numeric key, then the element values ​​will be transferred to $callback in sequence, otherwise it will transfer itself 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 be an array!');
  67. if(!is_callable($callback)) die ('parameter 2 shuld 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. ?>

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template