Convert a multidimensional array to XML

Please read the highlighted line below before commenting that this might be a duplicate. This has nothing to do with SimpleXML.

Let me begin by showing you how XML should be laid out. Please ignore namespaces:

<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> 

The notice under hot1: RoomsInformation is RoomInfo. I should be able to send multiple RoomInfo nodes. But I am using a PHP class to convert an array to this object to send via SOAP.

Here my array is converted to an object before it:

 $param = array( "Destination" => $destcode, "HotelCityName" => $city, "HotelLocationName" => "", "HotelName" => "", "CheckIn" => date("Ymd", strtotime($checkin)), "CheckOut" => date("Ymd", 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) ; 

The problem is that after converting to an object, there is only 1 RoomInfo and the last one. My thought is that the RoomsInformation array has 2 identical KEY names. So how can I do this job?

For your information, the SOAP class and the arrayToObject function are used here:

http://pastebin.com/SBUN0FAF

+4
source share
2 answers

The problem is that your array is invalid, as you suspected, due to duplicate keys. One way to solve the problem is to wrap each “RoomInfo” in its own array as follows:

 $param = array( "Destination" => $destcode, "HotelCityName" => $city, "HotelLocationName" => "", "HotelName" => "", "CheckIn" => date("Ymd", strtotime($checkin)), "CheckOut" => date("Ymd", 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" ); 

And you can generate XML as follows:

 // 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"); } } } 

I bind the array_to_xml function to a wonderful author here: fooobar.com/questions/28945 / ...

+7
source

It seems like you should have your array instead;

 $param = array( "Destination" => $destcode, "HotelCityName" => $city, "HotelLocationName" => "", "HotelName" => "", "CheckIn" => date("Ymd", strtotime($checkin)), "CheckOut" => date("Ymd", 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" ); 

This will save two elements of the RoomInfo array.

0
source

Source: https://habr.com/ru/post/1489290/


All Articles