连接来自不同SOAP方法的数据 [英] Join data from different SOAP methods
问题描述
我正在从几个SOAP方法中获取数据.使用simplexml_load_string()可以获得特定的数据,使用foreach循环可以显示该方法中的值.
I'm getting data from a couple of SOAP methods. With simplexml_load_string() I can get the specific data and with foreach loop I can display the values from that method.
这是我的代码的一部分,其中包含两种方法的结果:
Here's a part of my code for two methods results:
foreach($sxml1->NewDataSet->HotelFacility as $item) {
echo '<div class="hotel">';
echo '<div class="name">' . $item->FacName . '</div>';
echo '</div>';
}
foreach($sxml2->NewDataSet->HotelPresentation as $item) {
echo '<div class="hotel">';
echo '<div class="desc">' . $item->PresText . '</div>';
echo '</div>';
}
如何将这些值连接到一个主要的div酒店结构中,如下所示:
How can I join these values into a one main hotel div HTML structure like this:
<div class="hotel">
<div class="name">the_value_from_the_first_method</div>
<div class="desc">the_value_from_the_second_method</div>
</div>
<div class="hotel">
<div class="name">the_value_from_the_first_method</div>
<div class="desc">the_value_from_the_second_method</div>
</div>
<div class="hotel">
<div class="name">the_value_from_the_first_method</div>
<div class="desc">the_value_from_the_second_method</div>
</div>...
注意:作为SOAP方法的结果,div的数量将动态显示,因此结果必须从foreach循环中产生.
Notice: The number of divs will display dynamically as a result from a SOAP method, so the result has to be produced from foreach loop.
推荐答案
要合并来自单独的simpleXMLelement
对象的值(使用array_map
函数的解决方案):
To combine values from separate simpleXMLelement
objects (solution with array_map
function):
$sxml1 = simplexml_load_string($xml1);
$sxml2 = simplexml_load_string($xml2);
$hotelFac = $hotelPres = [];
foreach($sxml1->NewDataSet->HotelFacility as $item) {
$hotelFac[] = '<div class="hotel"><div class="name">' . $item->FacName . '</div>';
}
foreach($sxml2->NewDataSet->HotelPresentation as $item) {
$hotelPres[] = '<div class="desc">' . $item->PresText . '</div></div>';
}
$result = array_map(function($name, $desc){
return $name . $desc;
}, $hotelFac, $hotelPres);
echo implode('', $result);
http://php.net/manual/en/function.array- map.php
这篇关于连接来自不同SOAP方法的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!