嗨我试图用laravel 5.2制作一个xml文件。
我的功能我试过注释我试过,错误的xml解析。
public function createXML() { $maps = Maps::get()->toArray(); // function array_to_xml(array $arr, SimpleXMLElement $xml) // { // foreach ($arr as $k => $v) { // is_array($v) // ? array_to_xml($v, $xml->addChild($k)) // : $xml->addChild($k, $v); // } // return $xml; // } function to_xml(SimpleXMLElement $object, array $data) { foreach ($data as $key => $value) { if (is_array($value)) { $new_object = $object->addChild($key); to_xml($new_object, $value); } else { $object->addChild($key, $value); } } } // $xmloutput = to_xml($maps, new SimpleXMLElement('<root/>'))->asXML(); $xml = new SimpleXMLElement('<rootTag/>'); to_xml($xml, $maps); return Response::make($xml->asXML())->header('Content-Type', 'text/xml');
我的输出数组:
array:2 [▼ 0 => array:5 [▼ "id" => 1 "name" => "Leeuwarden" "address" => "Leeuwarden" "lat" => 53.20132 "lng" => 5.80005 ] 1 => array:5 [▼ "id" => 2 "name" => "Assen" "address" => "Assen" "lat" => 52.99275 "lng" => 6.56423 ] ]关于我做错了什么的任何建议?
我正在尝试使用数据库中的指针制作谷歌地图API地图。
Hi im trying to make a xml file with laravel 5.2.
My function What i commented out i tried, wrong xml parse.
public function createXML() { $maps = Maps::get()->toArray(); // function array_to_xml(array $arr, SimpleXMLElement $xml) // { // foreach ($arr as $k => $v) { // is_array($v) // ? array_to_xml($v, $xml->addChild($k)) // : $xml->addChild($k, $v); // } // return $xml; // } function to_xml(SimpleXMLElement $object, array $data) { foreach ($data as $key => $value) { if (is_array($value)) { $new_object = $object->addChild($key); to_xml($new_object, $value); } else { $object->addChild($key, $value); } } } // $xmloutput = to_xml($maps, new SimpleXMLElement('<root/>'))->asXML(); $xml = new SimpleXMLElement('<rootTag/>'); to_xml($xml, $maps); return Response::make($xml->asXML())->header('Content-Type', 'text/xml');my output array:
array:2 [▼ 0 => array:5 [▼ "id" => 1 "name" => "Leeuwarden" "address" => "Leeuwarden" "lat" => 53.20132 "lng" => 5.80005 ] 1 => array:5 [▼ "id" => 2 "name" => "Assen" "address" => "Assen" "lat" => 52.99275 "lng" => 6.56423 ] ]Any suggestions on what i am doing wrong?
i'm trying to make a google maps API map with pointers from the database.
最满意答案
第一个选项:直接在地图中获取正确的结构并命名数组。
第二个选项:如果你不能更改收到的$maps数组,那么你可以通过添加检查来修改头部级别:
$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();编辑:我检查了文档 ,似乎第一级需要命名为marker 。
1st option: get the right structure directly in maps and name the arrays.
2nd option: if you can'y change the $maps array received, then you can modify 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();Edit: I checked the docs, it seems, that the 1st level needs to be named marker.
更多推荐
发布评论