3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $context = [ 'mes' => [ 'attributes' => [ 't' => 'AddEvent', 'id' => 'test_id', ], 'ev' => [ 'attributes' => [ 'evt' => 'packet20130828ki3si0', 'src' => 'NVV', 'st' => 'test', 'start' => '12423511247', 'stop' => '12423991978', 'addrtype' => 'MB', 'clntID' => '+380957700418', 'lg' => '', 'bank' => '', 'lang' => '', 'prior' => '', 'brnm' => '', 'extref' => '', 'intref' => '', 'extaddrtype' => '', 'extaddrid' => '', 'recall' => '', ], 'attr' => [ [ 'attributes' => [ 'code' => '1', 'val' => '11', ], ], [ 'attributes' => [ 'code' => '2', 'val' => '12', ], ], ], 'hist' => [ [ 'attributes' => [ 'st' => '', 'ch' => '', 'lg' => '', 'con' => '', 'com' => '', 'addrtype' => '', 'addrid' => '', 'dt' => '', 'contactId' => '', ], ], ], ], ], ]; class Conv { protected $res; protected $context; public function __construct(array $context) { if (count($context) != 1) { throw new Exception('Must be only one root element', 500); } $this->context = $context; $this->init(key($this->context)); } public function conv() { return $this->a2x(current($this->context), $this->res)->asXML(); } protected function init($root) { $this->res = new SimpleXMLElement("<{$root}/>"); } protected function a2x(array $inner, SimpleXMLElement $node) { foreach ($inner as $key => $value) { if ($key == 'attributes') { if (count($value)) { foreach ($value as $attrKey => $attrVal) { var_dump($attrKey, $attrVal); $node->addAttribute($attrKey, $attrVal); } } } else { $subNode = $node->addChild($key); $this->a2x($value, $subNode); } } return $this->res; } } $a2x = new Conv($context); echo $a2x->conv()->asXML();
Output for 7.0.0 - 7.1.0
string(1) "t" string(8) "AddEvent" string(2) "id" string(7) "test_id" string(3) "evt" string(20) "packet20130828ki3si0" string(3) "src" string(3) "NVV" string(2) "st" string(4) "test" string(5) "start" string(11) "12423511247" string(4) "stop" string(11) "12423991978" string(8) "addrtype" string(2) "MB" string(6) "clntID" string(13) "+380957700418" string(2) "lg" string(0) "" string(4) "bank" string(0) "" string(4) "lang" string(0) "" string(5) "prior" string(0) "" string(4) "brnm" string(0) "" string(6) "extref" string(0) "" string(6) "intref" string(0) "" string(11) "extaddrtype" string(0) "" string(9) "extaddrid" string(0) "" string(6) "recall" string(0) "" string(10) "attributes" array(2) { ["code"]=> string(1) "1" ["val"]=> string(2) "11" } Warning: SimpleXMLElement::addAttribute() expects parameter 2 to be string, array given in /in/Ns4JV on line 91 string(4) "code" string(1) "2" string(3) "val" string(2) "12" string(10) "attributes" array(9) { ["st"]=> string(0) "" ["ch"]=> string(0) "" ["lg"]=> string(0) "" ["con"]=> string(0) "" ["com"]=> string(0) "" ["addrtype"]=> string(0) "" ["addrid"]=> string(0) "" ["dt"]=> string(0) "" ["contactId"]=> string(0) "" } Warning: SimpleXMLElement::addAttribute() expects parameter 2 to be string, array given in /in/Ns4JV on line 91 Fatal error: Uncaught Error: Call to a member function asXML() on string in /in/Ns4JV:105 Stack trace: #0 {main} thrown in /in/Ns4JV on line 105
Process exited with code 255.
Output for 5.6.0 - 5.6.28
string(1) "t" string(8) "AddEvent" string(2) "id" string(7) "test_id" string(3) "evt" string(20) "packet20130828ki3si0" string(3) "src" string(3) "NVV" string(2) "st" string(4) "test" string(5) "start" string(11) "12423511247" string(4) "stop" string(11) "12423991978" string(8) "addrtype" string(2) "MB" string(6) "clntID" string(13) "+380957700418" string(2) "lg" string(0) "" string(4) "bank" string(0) "" string(4) "lang" string(0) "" string(5) "prior" string(0) "" string(4) "brnm" string(0) "" string(6) "extref" string(0) "" string(6) "intref" string(0) "" string(11) "extaddrtype" string(0) "" string(9) "extaddrid" string(0) "" string(6) "recall" string(0) "" string(10) "attributes" array(2) { ["code"]=> string(1) "1" ["val"]=> string(2) "11" } Warning: SimpleXMLElement::addAttribute() expects parameter 2 to be string, array given in /in/Ns4JV on line 91 string(4) "code" string(1) "2" string(3) "val" string(2) "12" string(10) "attributes" array(9) { ["st"]=> string(0) "" ["ch"]=> string(0) "" ["lg"]=> string(0) "" ["con"]=> string(0) "" ["com"]=> string(0) "" ["addrtype"]=> string(0) "" ["addrid"]=> string(0) "" ["dt"]=> string(0) "" ["contactId"]=> string(0) "" } Warning: SimpleXMLElement::addAttribute() expects parameter 2 to be string, array given in /in/Ns4JV on line 91 Fatal error: Call to a member function asXML() on string in /in/Ns4JV on line 105
Process exited with code 255.
Output for 5.4.0 - 5.5.38
string(1) "t" string(8) "AddEvent" string(2) "id" string(7) "test_id" string(3) "evt" string(20) "packet20130828ki3si0" string(3) "src" string(3) "NVV" string(2) "st" string(4) "test" string(5) "start" string(11) "12423511247" string(4) "stop" string(11) "12423991978" string(8) "addrtype" string(2) "MB" string(6) "clntID" string(13) "+380957700418" string(2) "lg" string(0) "" string(4) "bank" string(0) "" string(4) "lang" string(0) "" string(5) "prior" string(0) "" string(4) "brnm" string(0) "" string(6) "extref" string(0) "" string(6) "intref" string(0) "" string(11) "extaddrtype" string(0) "" string(9) "extaddrid" string(0) "" string(6) "recall" string(0) "" string(10) "attributes" array(2) { ["code"]=> string(1) "1" ["val"]=> string(2) "11" } Warning: SimpleXMLElement::addAttribute() expects parameter 2 to be string, array given in /in/Ns4JV on line 91 string(4) "code" string(1) "2" string(3) "val" string(2) "12" string(10) "attributes" array(9) { ["st"]=> string(0) "" ["ch"]=> string(0) "" ["lg"]=> string(0) "" ["con"]=> string(0) "" ["com"]=> string(0) "" ["addrtype"]=> string(0) "" ["addrid"]=> string(0) "" ["dt"]=> string(0) "" ["contactId"]=> string(0) "" } Warning: SimpleXMLElement::addAttribute() expects parameter 2 to be string, array given in /in/Ns4JV on line 91 Fatal error: Call to a member function asXML() on a non-object in /in/Ns4JV on line 105
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/Ns4JV on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/Ns4JV on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/Ns4JV on line 3
Process exited with code 255.