如何使用SimpleXMLElement正确地对PHP变量进行类型转换?

时间:2012-01-14 04:11:28

标签: php casting php-5.3

我从XML Feed中提取数据并使用非常棒的SimpleXMLElement对其进行操作。但是,当我拉一个严格数字的节点时,我无法回显该节点的值。

经过调查,这里的评论http://www.php.net/manual/en/class.simplexmlelement.php#100162提到PHP有时会错误地对这些节点进行类型转换。但是,当我将变量强制转换为字符串时,它仍然不会出现。代码如下:

$wAresult = $wunderAXmlObject->xpath("//moon_phase");

$current_sunrise = (string) $wAresult->sunrise[0]->hour[0]
    . ':' . (string) $wAresult->sunrise[0]->minute[0];

有些人可以解释一下如何正确地进行类型转换,这样才能正常工作吗?谢谢!

编辑:我做了一个var_dump()来帮助诊断问题。奇怪的是,这些数字似乎被设置为字符串。此时我不确定我是否正确访问元素,但我会继续更新此问题。

array(1) {
    [0]=> object(SimpleXMLElement)#336 (5) {
        ["percentIlluminated"]=> string(2) "74"
        ["ageOfMoon"]=> string(2) "20"
        ["current_time"]=> object(SimpleXMLElement)#331 (2) {
            ["hour"]=> string(2) "20"
            ["minute"]=> string(2) "34"
        }
        ["sunset"]=> object(SimpleXMLElement)#337 (2) {
            ["hour"]=> string(2) "16"
            ["minute"]=> string(2) "39"
        }
        ["sunrise"]=> object(SimpleXMLElement)#338 (2) {
            ["hour"]=> string(1) "8"
            ["minute"]=> string(2) "03"
        }
    }
}

1 个答案:

答案 0 :(得分:1)

最有可能是$wAresult[0]->sunrise->hour