本文介绍了组合来自不同SOAP方法的循环值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从不同的SOAP方法获取数据.例如:

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

如果我print_r $ sxml,$ sxml2和$ sxml3,在里面可以获取任何数据,可以说

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

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

棘手的部分是这些值来自不同的方法,因此如何将它们组合在同一HTML结构中的循环中,如下所示:

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>

这是我想要实现的扩展示例.两种方法的示例.

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

print_r(sxml)显示了这样的结构:

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

第二个soap方法中的

print_r(sxml2)显示了这样的结构:

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

我如何合并这两个数组并获得结果,假设我要制作一个如下的html结构:

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>

HTML文章将动态显示,具体取决于SOAP方法中有多少个结果.

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

推荐答案

怎么样:

$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>';
}

当然,假设可以这样将HotelFacility和HotelPresentation数组元素链接在一起(通过它们的索引位置).根据您的解释很难建立两者之间的关系.

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.

这篇关于组合来自不同SOAP方法的循环值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 07:01