Combine loop values from different SOAP methods

狂风中的少年 提交于 2019-12-23 23:01:20

问题


I'm getting data from different SOAP methods. For an example:

$wsdl = 'the_url_of_wsdl';
$client = new SoapClient($wsdl); 
$parameters = array(all_the_parameters_inside);
$values = $client->the_method($parameters);
$xml = $values->the_methodResult->any; 
$sxml = simplexml_load_string($xml); 

$wsdl2 = 'the_url2_of_wsdl';
$client2 = new SoapClient($wsdl2); 
$parameters2 = array(all_the_parameters_inside);
$values2 = $client->the_method2($parameters2);
$xml2 = $values2->the_method2Result->any; 
$sxml2 = simplexml_load_string($xml2);

$wsdl3 = 'the_url3_of_wsdl';
$client3 = new SoapClient($wsdl3); 
$parameters3 = array(all_the_parameters_inside);
$values3 = $client->the_method3($parameters3);
$xml3 = $values3->the_method3Result->any; 
$sxml3 = simplexml_load_string($xml3);

If I print_r $sxml, $sxml2 and $sxml3, inside I can get any data, lets say

echo $sxml->name; , echo $sxml2->id; and echo $sxml3->description;.

The tricky part is these values are from different methods, so how can I combine them in loops in a same HTML structure, so to be like this:

<div class="article">
   <div class="name">the value from the first method</div>
   <div class="id">the value from the second method</div>
   <div class="description">the value from the third method</div>
</div>

<div class="article">
   <div class="name">the value from the first method</div>
   <div class="id">the value from the second method</div>
   <div class="description">the value from the third method</div>
</div>

<div class="article">
   <div class="name">the value from the first method</div>
   <div class="id">the value from the second method</div>
   <div class="description">the value from the third method</div>
</div>

EDIT: Here's extended example what I'm trying to achieve. An example of two methods.

print_r(sxml) from the first soap method shows a structure like this:

SimpleXMLElement Object
(
    [NewDataSet] => SimpleXMLElement Object
        (
            [HotelFacility] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [HotelId] => 1
                            [HotelCode] => TEST
                            [FacNo] => 6
                            [FacName] => test (bank)
                            [OriginalName] => 
                            [IsPriced] => No
                            [MediaID] => 1
                            [Note] => SimpleXMLElement Object
                                (
                                )

                            [UseinWebFilter] => true
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [HotelId] => 1
                            [HotelCode] => TEST2
                            [FacNo] => 12
                            [FacName] => test center
                            [OriginalName] => Test Center
                            [IsPriced] => No
                            [MediaID] => 1
                            [Note] => SimpleXMLElement Object
                                (
                                )

                            [UseinWebFilter] => true
                        ) //and the list continues...

print_r(sxml2) from the second soap method shows a structure like this:

SimpleXMLElement Object
(
    [NewDataSet] => SimpleXMLElement Object
        (
            [HotelPresentation] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [RecId] => 5
                            [HotelId] => 14
                            [HotelCode] => test hotel
                            [PresText] => "the description".
                            [TextType] => HTML
                            [TextCategory] => GENERAL
                            [MediaID] => 4
                            [CrtDate] => 2016-01-06T15:34:00+02:00
                            [ChgDate] => 2016-01-06T15:34:00+02:00
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [RecId] => 3
                            [HotelId] => 4
                            [HotelCode] => test hotel2
                            [PresText] => //and the list continues....

How do I merge these two arrays and get the result, let's say I want to make a html structure like this:

<div class="article">
   <div class="name">the value facName from the first method</div>
   <div class="id">the value hotelCode from the second method</div>
</div>

<div class="article">
   <div class="name">the value facName from the first method</div>
   <div class="id">the value hotelCode from the second method</div>
</div>

<div class="article">
   <div class="name">the value facName from the first method</div>
   <div class="id">the value hotelCode from the second method</div>
</div>

The HTML article will display dynamically depends how many results are in SOAP methods.


回答1:


How about:

$sxml1 = simplexml_load_string($xml1);
$sxml2 = simplexml_load_string($xml2);

$facilities = $sxml1->NewDataSet->HotelFacility;
$presentations = $sxml2->NewDataSet->HotelPresentation;

foreach ($facilities as $i => $facility) {
    echo '<div class="article">';
    echo '<div class="name">'. $facility->FacName .'</div>';
    echo '<div class="id">'. $presentations[$i]->HotelCode .'</div>';
    echo '</div>';
}

Assuming, of course, that HotelFacility and HotelPresentation array elements can be linked together like this (by their index position). It's kind of hard to establish the relationship between the two based on your explanation.



来源:https://stackoverflow.com/questions/35418407/combine-loop-values-from-different-soap-methods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!