iT邦幫忙

0

PHP 轉XML

date_default_timezone_set('Asia/Taipei');
$hour=date("H");
define("TW_LAT","Hello world!");
define("TW_LON","Hello world!");
$exec_t1 = microtime(true);



$lat = '25.0641148';
$lon = '121.524200999999';
$distance = 0.01; //init search distance(1.5km)
$return = array();
$exec_t2 = microtime(true);
$return = search_nearest($lat, $lon, $distance, $uuid, $major, $minor, $hour);
$exec_t3 = microtime(true);
function search_nearest($lat, $lon, $distance, $uuid, $major, $minor, $hour){
	
        include("./foot_include/footglobal.php");
				if($uuid!="" && $major!="" && $minor!=""){
					include("./foot_include/db_connect_70.php");
					$insert_bracelet = "INSERT INTO beacon_log (uuid, major, minor, latitude, longitude, os_type, update_time, updatetime_hour) values ('$uuid', '$major', '$minor', '$lat', '$lon','web', NOW(), '$hour')";
					mysql_query($insert_bracelet, $connect_70) or die(mysql_error());
				}


	$query_nearest = "SELECT * from tbl_Neighborhood where
                            lat > ".$lat."-".$distance." AND
                            lat < ".$lat."+".$distance." and
                            lon > ".$lon."-".$distance." AND
                            lon < ".$lon."+".$distance."
                            ORDER BY ACOS(SIN((".$lat." * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((".$lat." * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((".$lon."* 3.1415) / 180 - (lon * 3.1415) / 180 ) ) * 6380
                            asc limit 1";
	$result_nearest = mysql_query($query_nearest, $connect);
	if($row = mysql_fetch_assoc($result_nearest) ){
		$city = $row['city'];
	        $area = $row['area'];
        	$name = $row['neighborhood'];
	        $admin = $row['admin'];
        	$phone = $row['phone'];
	        $photo = $row['photo'];
        	$address = $row['address'];
	        $return = array('msg' => 'done' ,'city' => $city , 'area' => $area , 'name' => $name , 'admin' => $admin , 'phone' => $phone , 'photo' => $photo , 'address' => $address);
	        

	}else{
		$return = search_nearest($lat, $lon, $distance*2);
	}

		return $return;
}

$exec_t4 = microtime(true);

$exec_time1 = round(($exec_t2 - $exec_t1) * 1000, 3);
$exec_time2 = round(($exec_t3 - $exec_t2) * 1000, 3);
$exec_time3 = round(($exec_t4 - $exec_t3) * 1000, 3);
mysql_close();

$today = mktime(0,0,0,date("m"),date("d"), date("Y"));
$date = date("Y-m-d", $today);
$fp = fopen("log/scan_nearest_$date", 'a');
fwrite($fp, "=================\n");
fwrite($fp, "SCAN START\n");
fwrite($fp, "time1: $exec_time1\n");
fwrite($fp, "time2: $exec_time2\n");
fwrite($fp, "time3: $exec_time3\n");
fwrite($fp, "SCAN end\n");
fwrite($fp, "=================\n");

想請問我要把抓出來的$return的array 資料轉成xml....該怎麼處理呢.....
試了很多方式但都轉不出來

看更多先前的討論...收起先前的討論...
q00153 iT邦新手 3 級 ‧ 2016-05-27 14:41:32 檢舉
PHP 有 SimpleXML 物件,可以用來轉~

參考網頁如下

http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml
yugochen iT邦新手 5 級 ‧ 2016-05-27 16:22:30 檢舉
我轉出來後呈現msg<臺北市>city<中山區>area<恒安里>name<黃志昌>admin<0225853352>phonephoto<(104)台北市中山區恆安里雙城街10巷8號>address
這樣是對的嗎??
不對!



台北市


中山區

.........


這樣的格式
```

臺北市
中山區
恒安里
黃志昌
0225853352
(104)台北市中山區恆安里雙城街10巷8號


```

臺北市
中山區
恒安里
黃志昌
0225853352
(104)台北市中山區恆安里雙城街10巷8號

不好意思程式碼打不上來~~!
-msg ]
-city ]臺北市-/city]
-area ]中山區-/area]
-name ]恒安里-/name]
-admin ]黃志昌-/admin]
-phonephoto ]0225853352-/phonephoto]
-address ](104)台北市中山區恆安里雙城街10巷8號-/address]
-/msg ]

- 取代成 <
] 取代成>
weiclin iT邦高手 4 級 ‧ 2016-05-27 20:01:09 檢舉
test
weiclin iT邦高手 4 級 ‧ 2016-05-27 20:01:44 檢舉
不能貼 tag 是要討論什麼 -_-
yugochen iT邦新手 5 級 ‧ 2016-05-30 09:42:09 檢舉
```
$lat = '25.0653714';
$lon = '121.5229885';
$distance = 0.01; //init search distance(1.5km)
$return = array();
$exec_t2 = microtime(true);
$return = search_nearest($lat, $lon, $distance, $uuid, $major, $minor, $hour);
$exec_t3 = microtime(true);
function search_nearest($lat, $lon, $distance, $uuid, $major, $minor, $hour){

include("./foot_include/footglobal.php");
if($uuid!="" && $major!="" && $minor!=""){
include("./foot_include/db_connect_70.php");
$insert_bracelet = "INSERT INTO beacon_log (uuid, major, minor, latitude, longitude, os_type, update_time, updatetime_hour) values ('$uuid', '$major', '$minor', '$lat', '$lon','web', NOW(), '$hour')";
mysql_query($insert_bracelet, $connect_70) or die(mysql_error());
}


$query_nearest = "SELECT * from tbl_Neighborhood where
lat > ".$lat."-".$distance." AND
lat < ".$lat."+".$distance." and
lon > ".$lon."-".$distance." AND
lon < ".$lon."+".$distance."
ORDER BY ACOS(SIN((".$lat." * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((".$lat." * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((".$lon."* 3.1415) / 180 - (lon * 3.1415) / 180 ) ) * 6380
asc limit 1";
$result_nearest = mysql_query($query_nearest, $connect);
if($row = mysql_fetch_assoc($result_nearest) ){
$city = $row['city'];
$area = $row['area'];
$name = $row['neighborhood'];
$admin = $row['admin'];
$phone = $row['phone'];
$photo = $row['photo'];
$address = $row['address'];
$return = array('msg' => 'done' ,'city' => $city , 'area' => $area , 'name' => $name , 'admin' => $admin , 'phone' => $phone , 'photo' => $photo , 'address' => $address);

}else{
$return = search_nearest($lat, $lon, $distance*2);

}

return $return;
}
$xml = new SimpleXMLElement('');
array_walk_recursive($return, array ($xml, 'addChild'));
print $xml->asXML();

$exec_t4 = microtime(true);

$exec_time1 = round(($exec_t2 - $exec_t1) * 1000, 3);
$exec_time2 = round(($exec_t3 - $exec_t2) * 1000, 3);
$exec_time3 = round(($exec_t4 - $exec_t3) * 1000, 3);
mysql_close();

$today = mktime(0,0,0,date("m"),date("d"), date("Y"));
$date = date("Y-m-d", $today);
$fp = fopen("log/scan_nearest_$date", 'a');
fwrite($fp, "=================\n");
fwrite($fp, "SCAN START\n");
fwrite($fp, "time1: $exec_time1\n");
fwrite($fp, "time2: $exec_time2\n");
fwrite($fp, "time3: $exec_time3\n");
fwrite($fp, "SCAN end\n");
fwrite($fp, "=================\n");
```
yugochen iT邦新手 5 級 ‧ 2016-05-30 09:43:14 檢舉
請問我照著SimpleXML 物件,用來轉,是哪裡寫錯了嗎??
yugochen iT邦新手 5 級 ‧ 2016-05-30 09:44:23 檢舉
請問不能貼tag討論 是我沒有設定到什麼嗎??
我有時間寫一個範例給你
http://ithelp.ithome.com.tw/articles/10183590
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

4
老鷹(eagle)
iT邦高手 1 級 ‧ 2016-05-27 16:11:41
最佳解答

我要發表回答

立即登入回答