2013-07-02 147 views
4

请先阅读下面的粗体行,然后再评论这可能是重复的。这与SimpleXML无关。将多维数组转换为XML

让我首先展示如何布置XML。请忽略名称空间:

<hot:SearchHotels> 
    <hot:request> 
     <hot1:Destination>?</hot1:Destination> 
     <hot1:HotelCityName>?</hot1:HotelCityName> 
     <hot1:HotelLocationName>?</hot1:HotelLocationName> 
     <hot1:HotelName>?</hot1:HotelName> 
     <hot1:CheckIn>?</hot1:CheckIn> 
     <hot1:CheckOut>?</hot1:CheckOut> 
     <hot1:RoomsInformation> 
      <!--Zero or more repetitions:--> 
      <hot1:RoomInfo> 
       <hot1:AdultNum>?</hot1:AdultNum> 
       <hot1:ChildNum>?</hot1:ChildNum> 
       <!--Optional:--> 
       <hot1:ChildAges> 
       <!--Zero or more repetitions:--> 
       <hot1:ChildAge age="?"/> 
       </hot1:ChildAges> 
      </hot1:RoomInfo> 
     </hot1:RoomsInformation> 
     <hot1:MaxPrice>?</hot1:MaxPrice> 
     <hot1:StarLevel>?</hot1:StarLevel> 
     <hot1:AvailableOnly>?</hot1:AvailableOnly> 
     <hot1:PropertyType>?</hot1:PropertyType> 
     <hot1:ExactDestination>?</hot1:ExactDestination> 
    </hot:request> 
    </hot:SearchHotels> 

在hot1下的通知:RoomsInformation里面有RoomInfo。我应该能够发送多个RoomInfo节点。 但我正在使用PHP类将数组转换为通过SOAP提交的此对象。

这里是我的阵列之前,它被转换为对象:

$param = array(
      "Destination" => $destcode, 
      "HotelCityName" => $city, 
      "HotelLocationName" => "", 
      "HotelName" => "", 
      "CheckIn" => date("Y-m-d", strtotime($checkin)), 
      "CheckOut" => date("Y-m-d", strtotime($checkout)), 
      "RoomsInformation" => array (
       "RoomInfo" => array(
         "AdultNum" => 2, 
         "ChildNum" => 1, 
         "ChildAges" => array(
          "ChildAge" => array(
           "age"=>11 
          ) 
         ) 
        ), 
       "RoomInfo" => array(
         "AdultNum" => 1, 
         "ChildNum" => 0, 
         "ChildAges" => array(
          "ChildAge" => array(
           "age"=>0 
          ) 
         ) 
        ) 
      ), 
      "MaxPrice" => 0, 
      "StarLevel" => 0, 
      "AvailableOnly" => "false", 
      "PropertyType" => "NotSet", 
      "ExactDestination" => "false" 
     ); 

$param = arrayToObject($param) ; 
$obj = new stdClass(); 
$obj->request=$param; 
$result = $test->SearchHotels($obj) ; 

的问题是转换为对象之后,只有1 RoomInfo和最后一个。我的想法是因为RoomsInformation数组有2个相同的KEY名称。那我该如何做这项工作?

为了您的信息,这里是SOAP I类使用和arrayToObject功能:

http://pastebin.com/SBUN0FAF

+3

[如何将数组转换为SimpleXML](http://stackoverflow.com/q/1397036/1503018) – sectus

+1

如果使用SOAP,那么php内置的SoapClient可以处理数组 – Fracsi

+2

您可以'吨,而不是该阵列。不管你有多少个'RoomsInformation'条目,如果他们有相同的密钥,他们确实只有一个。 –

回答

7

的问题是,你的阵列是无效的,因为你,因为重复键的怀疑。解决这个问题的方法之一是包装每个“RoomInfo”在自己的数组,像这样:

$param = array(
    "Destination" => $destcode, 
    "HotelCityName" => $city, 
    "HotelLocationName" => "", 
    "HotelName" => "", 
    "CheckIn" => date("Y-m-d", strtotime($checkin)), 
    "CheckOut" => date("Y-m-d", strtotime($checkout)), 
    "RoomsInformation" => array (
     array(
      "RoomInfo" => array(
       "AdultNum" => 2, 
       "ChildNum" => 1, 
       "ChildAges" => array(
        "ChildAge" => array(
         "age"=>11 
        ) 
       ) 
      ), 
     ), 
     array(
      "RoomInfo" => array(
       "AdultNum" => 1, 
       "ChildNum" => 0, 
       "ChildAges" => array(
        "ChildAge" => array(
         "age"=>0 
        ) 
       ) 
      ) 
     ) 
    ), 
    "MaxPrice" => 0, 
    "StarLevel" => 0, 
    "AvailableOnly" => "false", 
    "PropertyType" => "NotSet", 
    "ExactDestination" => "false" 
); 

而且你可以生成XML这样的:

// create simpleXML object 
$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><SearchHotels></SearchHotels>"); 
$node = $xml->addChild('request'); 

// function call to convert array to xml 
array_to_xml($param, $node); 

// display XML to screen 
echo $xml->asXML(); 
die(); 

// function to convert an array to XML using SimpleXML 
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 { 
       array_to_xml($value, $xml); 
      } 
     } else { 
      $xml->addChild("$key","$value"); 
     } 
    } 
} 

我属性array_to_xml功能的美妙的作者在这里:https://stackoverflow.com/a/5965940/2200766

+1

不试图使用SimpleXML。我必须转换为通过SOAP客户端发送的对象。进行您所建议的更改会使两个RoomsInfo完全消失。 – swg1cor14

+0

请参阅我添加到问题末尾的修改 – swg1cor14

0

它看起来好像你应该有你这样的数组,而不是;

$param = array(
     "Destination" => $destcode, 
     "HotelCityName" => $city, 
     "HotelLocationName" => "", 
     "HotelName" => "", 
     "CheckIn" => date("Y-m-d", strtotime($checkin)), 
     "CheckOut" => date("Y-m-d", strtotime($checkout)), 
     "RoomsInformation" => array (
      "RoomInfo" => array(
        array(
        "AdultNum" => 2, 
        "ChildNum" => 1, 
        "ChildAges" => array(
         "ChildAge" => array(
          "age"=>11 
         ) 
        ) 
       ), 
        array(
        "AdultNum" => 1, 
        "ChildNum" => 0, 
        "ChildAges" => array(
         "ChildAge" => array(
          "age"=>0 
         ) 
        ) 
       ) 
      ) 
     ), 
     "MaxPrice" => 0, 
     "StarLevel" => 0, 
     "AvailableOnly" => "false", 
     "PropertyType" => "NotSet", 
     "ExactDestination" => "false" 
    ); 

这将保留两个RoomInfo数组元素。