user2519032
user2519032

Reputation: 829

Join data from different SOAP methods

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

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

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.

Upvotes: 1

Views: 90

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

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

Upvotes: 2

Related Questions