3v4l.org

run code in 300+ PHP versions simultaneously
<?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);

preferences:
42.18 ms | 402 KiB | 5 Q