3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $myArray = array( 'Europe' => 'World', 'Asia' => 'World', 'Africa' => 'World', 'France' => 'Europe', 'Poland' => 'Europe', 'Germany' => 'Europe', 'Paris' => 'France', 'Lille' => 'France', 'Warsaw' => 'Poland', 'Szczecin' => 'Poland', 'Berlin' => 'Germany', 'Gumience' => 'Szczecin' ); $x = new SimpleXMLElement ("<root/>"); foreach ($myArray as $child => $parent) { if (!$x->xpath ("//$parent")) { $x->addChild($parent); } $x->xpath ("//$parent")[0]->addChild($child); } $json = json_encode($x); $array = json_decode($json,TRUE); var_export ($array);
Output for 5.4.28 - 5.4.45, 5.5.12 - 7.2.0
array ( 'World' => array ( 'Europe' => array ( 'France' => array ( 'Paris' => array ( ), 'Lille' => array ( ), ), 'Poland' => array ( 'Warsaw' => array ( ), 'Szczecin' => array ( 'Gumience' => array ( ), ), ), 'Germany' => array ( 'Berlin' => array ( ), ), ), 'Asia' => array ( ), 'Africa' => array ( ), ), )
Output for 5.4.0 - 5.4.27, 5.5.0 - 5.5.11
array ( 'World' => array ( 'Europe' => array ( 'France' => array ( 'Paris' => array ( ), 'Lille' => array ( ), ), 'Poland' => array ( 'Warsaw' => array ( ), 'Szczecin' => array ( 0 => array ( ), ), ), 'Germany' => array ( 0 => array ( ), ), ), 'Asia' => array ( ), 'Africa' => array ( ), ), )
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/fenJ6 on line 26
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/fenJ6 on line 26
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/fenJ6 on line 26
Process exited with code 255.