3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function fromXML(XMLReader $xml) { $node = []; while ($xml->read()) { // if (!$xml->isValid()) { // $xmlErrors = libxml_get_errors(); // $errorLines = array(); // foreach ($xmlErrors as $error) { // $errorLines[] = 'Line: ' . $error->line . ' Column: ' . $error->column . ' - ' . $error->message; // } // throw new Exception(join("", $errorLines)); // } switch ($xml->nodeType) { case XMLReader::END_ELEMENT: return (count($node) > 0) ? $node : null; case XMLReader::ELEMENT: $node[$xml->name] = ($xml->isEmptyElement ? '' : fromXML($xml)); break; case XMLReader::TEXT: case XMLReader::CDATA: $node = $xml->value; break; } } return $node; } $data = '<?xml version="1.0" encoding="UTF-8"?> <request> <header> <version>0.6</version> <dateTime>2017-07-05T11:21:46</dateTime> <sender>PROVIDER</sender> <recipient>dfsafdaf</recipient> <messageType>isvalid</messageType> </header> <voucher> <voucherId>1</voucherId> </voucher> </request>'; $xml = new XMLReader(); $xml->xml($data); var_dump(fromXML($xml));
Output for 7.1.7
array(1) { ["request"]=> array(2) { ["header"]=> array(5) { ["version"]=> string(3) "0.6" ["dateTime"]=> string(19) "2017-07-05T11:21:46" ["sender"]=> string(8) "PROVIDER" ["recipient"]=> string(8) "dfsafdaf" ["messageType"]=> string(7) "isvalid" } ["voucher"]=> array(1) { ["voucherId"]=> string(1) "1" } } }