PHP轉XML

  • 486
  • 0

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

IT邦問題 http://ithelp.ithome.com.tw/questions/10183369

問題

<?php
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");

我的回答

<?php

/**
 * @note array to XML example 160607 by Eagle(老鷹)
 * @for IT Help q: http://ithelp.ithome.com.tw/questions/10183369
 * @Reference material
 * url: http://www.codexworld.com/convert-array-to-xml-in-php/
 **/

/**
 * view can show xml tag
 */
header('Content-Type: text/plain');

/**
 * @get url:http://www.codexworld.com/convert-array-to-xml-in-php/
 * @param $array array
 * @param $xml new SimpleXMLElement
 */
function array_to_xml($array, &$xml)
{
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            if (!is_numeric($key)) {
                $subnode = $xml->addChild("$key");
                array_to_xml($value, $subnode);
            } else {
                $subnode = $xml->addChild("$key");
                array_to_xml($value, $subnode);
            }
        } else {
            $xml->addChild("$key", htmlspecialchars("$value"));
        }
    }
}

$data_array = [0 => ['name' => 'eagle', 'age' => 29]
    , 1 => ['name' => 'jack', 'age' => 60]];

$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><user_info></user_info>");
array_to_xml($data_array, $xml);
print_r($xml->asXML());

result

<?xml version="1.0"?>
    <user_info>
        <0>
           <name>eagle</name>
           <age>29</age>
        </0>
        <1>
           <name>jack</name>
           <age>60</age>
        </1>
    </user_info>