Reputation: 11
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
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
Reputation: 100175
you mean like:
$xmlObj = simplexml_load_string($your_xml_response);
foreach($xmlObj->HotelSummary as $hotel) {
$imgSrc = "$$server$$" . $hotel->thumbNailUrl;
}
Upvotes: 1
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