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, "
";
}