简体   繁体   中英

SimpleXML node casting to string not working

I am trying to get a currency rate from this XML file:

http://www.bank.lv/vk/xml.xml

I am getting a currency ID from a HTML form, after that I have to find it according currency rate.

I am using SimpleXML and XPath, my selection is as follow:

$current_rate = $rates->xpath("/CRates/Currencies/Currency[ID='" .$source_currency ."']/Rate");

$source_currency is tested and valid, however, when casting $current_rate to (string) , I get the word Array .

Do I have a mistake in the XPath node selection or somewhere else?

$current_rate = $rates->xpath("/CRates/Currencies/Currency[ID='" .$source_currency ."']/Rate");

Will return an array even if just 1 result is returned, if you use print_r you can see what is returned:

print_r($current_rate);

To access it you will have to use:

if (isset($current_rate))
{
    echo $current_rate[0];
}

Or if there is the possibility of having more than 1 result for that given $source_currency :

foreach ($current_rate as $rate)
{
    echo $rate, "\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM