fork download
  1. <?php
  2.  
  3. function XML2Array($xmlContent, $out = array()){
  4. $xmlObject = is_object($xmlContent) ? $xmlContent : simplexml_load_string($xmlContent);
  5. foreach((array) $xmlObject as $index => $node)
  6. $out[$index] = ( is_object($node) || is_array($node) ) ? XML2Array( $node ) : $node;
  7. return $out;
  8. }
  9.  
  10. $page = <<<FIM
  11. <?xml version='1.0' encoding='us-ascii'?>
  12.  
  13. <!-- A SAMPLE set of slides -->
  14.  
  15. <slideshow
  16.   title="Sample Slide Show"
  17.   date="Date of publication"
  18.   author="Yours Truly"
  19.   >
  20.  
  21.   <!-- TITLE SLIDE -->
  22.   <slide type="all">
  23.   <title>Wake up to WonderWidgets!</title>
  24.   </slide>
  25.  
  26.   <!-- OVERVIEW -->
  27.   <slide type="all">
  28.   <title>Overview</title>
  29.   <item>Why <em>WonderWidgets</em> are great</item>
  30.   <item/>
  31.   <item>Who <em>buys</em> WonderWidgets</item>
  32.   </slide>
  33.  
  34. </slideshow>
  35. FIM;
  36. $array = XML2Array($page);
  37. print_r($array);
Success #stdin #stdout #stderr 0.02s 24448KB
stdin
Standard input is empty
stdout
Array
(
    [@attributes] => Array
        (
        )

    [comment] => Array
        (
        )

    [slide] => Array
        (
        )

)
stderr
PHP Warning:  simplexml_load_string() expects parameter 1 to be string, array given in /home/KYDu4D/prog.php on line 4
PHP Warning:  simplexml_load_string() expects parameter 1 to be string, array given in /home/KYDu4D/prog.php on line 4
PHP Warning:  simplexml_load_string() expects parameter 1 to be string, array given in /home/KYDu4D/prog.php on line 4