0

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?

1 Answer 1

1
$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"; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, worked like a charm! I was just a little confused, because I looked it up, and everyone just seemed to be casting it straight to string. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.