antonio esposito
antonio esposito

Reputation: 11

Foreach xml server response

I have some questions about foreach. I do a request with curl and I receive a response in XML like:

<HotelList>
<HotelSummary id=848484>
<hotel>prova</hotel>
<address>via bho</address>
<thumbNailUrl>/hotels/1000000/530000/526200/526198/526198_38_t.jpg</thumbNailUrl>
</HotelSummary>
</HotelList>

How can I change every

<thumbNailUrl>/hotels/1000000/530000/526200/526198/526198_38_t.jpg</thumbNailUrl>

to

<img src="$$server$$/hotels/1000000/530000/526200/526198/526198_38_t.jpg>

Is it possible to do this with foreach?

Upvotes: 0

Views: 60

Answers (3)

Harsh Chunara
Harsh Chunara

Reputation: 585

There are 2 option for solving it

In foreach

foreach($xmlarray as $key => $val)
{   
    $fileInfo = pathinfo($val);
    if(in_array($fileInfo['extension'],array("jpg","jpeg","gif","png","tiff"))
    {
        $val = "<img src='".$serverpath.$val."' >";
    }
}

OR

foreach($xmlarray as $key => $val)
{   

    if(in_array( substr(strrchr($val,'.'),1),array("jpg","jpeg","gif","png","tiff"))
    {
        $val = "<img src='".$serverpath.$val."' >";
    }
}

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

you mean like:

$xmlObj = simplexml_load_string($your_xml_response);
foreach($xmlObj->HotelSummary as $hotel) {
  $imgSrc = "$$server$$" . $hotel->thumbNailUrl;
}

Upvotes: 1

feeela
feeela

Reputation: 29932

No, not with a foreach loop alone.
Yes, using a foreach loop is good way to start.

You could for example iterate over an SimpleXml object. Please refer to the PHP SimpleXML manual to get started with the basics. Always also have a look at the comments, as they provide a lot of examples.

Upvotes: 0

Related Questions