3v4l.org

run code in 300+ PHP versions simultaneously
<?php function xml_to_array($root) { $result = array(); if ($root->hasAttributes()) { $attrs = $root->attributes; foreach ($attrs as $attr) { $result['@attributes'][$attr->name] = $attr->value; } } if ($root->hasChildNodes()) { $children = $root->childNodes; if ($children->length == 1) { $child = $children->item(0); if ($child->nodeType == XML_TEXT_NODE) { $result['_value'] = $child->nodeValue; return count($result) == 1 ? $result['_value'] : $result; } } $groups = array(); foreach ($children as $child) { if (!isset($result[$child->nodeName])) { $result[$child->nodeName] = xml_to_array($child); } else { if (!isset($groups[$child->nodeName])) { $result[$child->nodeName] = array($result[$child->nodeName]); $groups[$child->nodeName] = 1; } $result[$child->nodeName][] = xml_to_array($child); } } } return $result; } $xml = "<Msg> <Txn>submitsms </Txn> <AccountID>123456789</AccountID> <Password>barfoo123</Password> <Message>test message</Message> <RateCode>1</RateCode> <Mobiles> <MobileNo>07123456701</MobileNo> </Mobiles> </Msg>"; $doc = new DOMDocument(); $doc->loadXML($xml); var_dump(xml_to_array($doc));
Output for 5.6.0 - 5.6.40, 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, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
array(1) { ["Msg"]=> array(7) { ["#text"]=> array(7) { [0]=> array(0) { } [1]=> array(0) { } [2]=> array(0) { } [3]=> array(0) { } [4]=> array(0) { } [5]=> array(0) { } [6]=> array(0) { } } ["Txn"]=> string(11) "submitsms " ["AccountID"]=> string(9) "123456789" ["Password"]=> string(9) "barfoo123" ["Message"]=> string(12) "test message" ["RateCode"]=> string(1) "1" ["Mobiles"]=> array(2) { ["#text"]=> array(2) { [0]=> array(0) { } [1]=> array(0) { } } ["MobileNo"]=> string(11) "07123456701" } } }

preferences:
148.65 ms | 405 KiB | 283 Q