1st option: get the correct structure specifically in maps and name the arrays.
2nd option: in the event that you can't change the $maps arrays got, at that point you can adjust the head level by adding check to it:
$maps = [ 0 => [ "id" => 1, "name" => "Leeuwarden", "address" => "Leeuwarden", "lat" => 53.20132, "lng" => 5.80005, ], 1 => [ "id" => 2, "name" => "Assen", "address" => "Assen", "lat" => 52.99275, "lng" => 6.56423, ] ];
function to_xml(\SimpleXMLElement $object, array $data, $level = 0) { foreach ($data as $key => $value) { if (is_array($value)) { $new_object = $object->addChild(($level == 0) ? 'marker' : $key); to_xml($new_object, $value, $level + 1); } else { $object->addChild($key, $value); } } }
$xml = new \SimpleXMLElement('<rootTag/>'); to_xml($xml, $maps);
header('Content-type: text/xml'); echo $xml->asXML();
By:Xtreem Solution
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community