3v4l.org

run code in 300+ PHP versions simultaneously
<?php $xml = <<<XML <cats> <cat> <name>Jack</name> <age>2</age> <color>grey</color> <color>white</color> </cat> <cat> <name>Maxwell</name> <age>12</age> <color>orange</color> <color>black</color> </cat> </cats> XML; function xml2array($fname){ $sxi = new SimpleXmlIterator($fname, null, true); return sxiToArray($sxi); } function sxiToArray($sxi){ $a = array(); for( $sxi->rewind(); $sxi->valid(); $sxi->next() ) { if(!array_key_exists($sxi->key(), $a)){ $a[$sxi->key()] = array(); } if($sxi->hasChildren()){ $a[$sxi->key()][] = sxiToArray($sxi->current()); } else{ $a[$sxi->key()][] = strval($sxi->current()); } } return $a; } // Read cats.xml and print the results: $catArray = xml2array($xml); print_r($catArray); ?>
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: SimpleXMLElement::__construct(): Passing null to parameter #2 ($options) of type int is deprecated in /in/Snet9 on line 21 Warning: SimpleXMLElement::__construct(): I/O warning : failed to load external entity "<cats> <cat> <name>Jack</name> <age>2</age> <color>grey</color> <color>white</color> </cat> <cat> <name>Maxwell</name> <age>12</age> <color>orange</color> <color>black</color> </cat> </cats>" in /in/Snet9 on line 21 Fatal error: Uncaught Exception: String could not be parsed as XML in /in/Snet9:21 Stack trace: #0 /in/Snet9(21): SimpleXMLElement->__construct('<cats>\n <cat>\n...', NULL, true) #1 /in/Snet9(42): xml2array('<cats>\n <cat>\n...') #2 {main} thrown in /in/Snet9 on line 21
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
Warning: SimpleXMLElement::__construct(): I/O warning : failed to load external entity "<cats> <cat> <name>Jack</name> <age>2</age> <color>grey</color> <color>white</color> </cat> <cat> <name>Maxwell</name> <age>12</age> <color>orange</color> <color>black</color> </cat> </cats>" in /in/Snet9 on line 21 Fatal error: Uncaught Exception: String could not be parsed as XML in /in/Snet9:21 Stack trace: #0 /in/Snet9(21): SimpleXMLElement->__construct('<cats>\n <cat>\n...', NULL, true) #1 /in/Snet9(42): xml2array('<cats>\n <cat>\n...') #2 {main} thrown in /in/Snet9 on line 21
Process exited with code 255.
Output for 5.5.11 - 5.5.38, 5.6.0 - 5.6.40
Warning: SimpleXMLElement::__construct(): I/O warning : failed to load external entity "<cats> <cat> <name>Jack</name> <age>2</age> <color>grey</color> <color>white</color> </cat> <cat> <name>Maxwell</name> <age>12</age> <color>orange</color> <color>black</color> </cat> </cats>" in /in/Snet9 on line 21 Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/Snet9:21 Stack trace: #0 /in/Snet9(21): SimpleXMLElement->__construct('<cats>\n <cat>\n...', 0, true) #1 /in/Snet9(42): xml2array('<cats>\n <cat>\n...') #2 {main} thrown in /in/Snet9 on line 21
Process exited with code 255.
Output for 5.5.0 - 5.5.10
Warning: SimpleXMLElement::__construct(): I/O warning : failed to load external entity "<cats> <cat> <name>Jack</name> <age>2</age> <color>grey</color> <color>white</color> </cat> <cat> <name>Maxwell</name> <age>12</age> <color>orange</color> <color>black</color> </cat> </cats>" in /in/Snet9 on line 21 Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/Snet9:21 Stack trace: #0 /in/Snet9(21): SimpleXMLElement->__construct('<cats>? <cat>?...', 0, true) #1 /in/Snet9(42): xml2array('<cats>? <cat>?...') #2 {main} thrown in /in/Snet9 on line 21
Process exited with code 255.

preferences:
260.33 ms | 402 KiB | 330 Q