Ben Ben - 4 months ago 30
PHP Question

PHP foreach not working XML

I don't really understand why this foreach is not working:

<?php
$url = "https://maps.googleapis.com/maps/api/place/textsearch/xml?query=hotels+in+newyork&key=AIzaSyAZAkutsge0U-xZVMvwf_Iw_ubdN2bra64";
$xml = simplexml_load_file($url);

foreach($xml->result->name as $name)
{

echo $name;
}


it shows only the 1st row!

Answer

you have to loop $xml->result.

foreach($xml->result as $result)
{
    echo $result->name;
}
Comments