简体   繁体   English

SimpleXML节点强制转换为字符串不起作用

[英]SimpleXML node casting to string not working

I am trying to get a currency rate from this XML file: 我正在尝试从此XML文件获取货币汇率:

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. 我从HTML表单中获取货币ID,之后必须根据货币汇率找到它。

I am using SimpleXML and XPath, my selection is as follow: 我正在使用SimpleXML和XPath,选择如下:

$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 . $source_currency已测试并且有效,但是,将$current_rate强制转换$current_rate (string) ,得到了Array这个词。

Do I have a mistake in the XPath node selection or somewhere else? XPath节点选择或其他地方是否有错误?

$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: 即使仅返回1个结果,也将返回一个数组,如果使用print_r ,则可以看到返回的结果:

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 : 或者,对于给定的$source_currency ,是否有可能有多个结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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