将SimpleXML节点值作为字符串访问

时间:2013-02-13 20:57:31

标签: php simplexml

我有一个名为$ picasa_feed的对象:

SimpleXMLElement Object
(
    [guid] => https://picasaweb.google.com/data/entry/base/user/0000000000000000/albumid/0000000000000000?alt=rss&hl=en_US
    [pubDate] => Fri, 12 Dec 2008 20:00:00 +0000
    [category] => http://schemas.google.com/photos/2007#album
    [title] => My Pictures
    [description] => ...
    [link] => https://picasaweb.google.com/0000000000000000/MyAlbum
    [author] => Me
)

我想将一个属性值放入一个关联数组的元素中:

$data_to_save['title'] = $picasa_feed->title;

当我这样做时,$ data_to_save的值是

Array
(
    [title] => SimpleXMLElement Object
        (
            [0] => My Pictures
        )
}

我想要的是

Array
(
    [title] => My Pictures
}

我做错了什么以及如何解决?

3 个答案:

答案 0 :(得分:1)

将其投射到字符串:

$data_to_save['title'] = (string) $picasa_feed->title;

答案 1 :(得分:0)

您希望通过将其转换为字符串来调用SimpleXMLElement的magic __toString方法。尝试:$data_to_save['title'] = (string)$picasa_feed->title;

答案 2 :(得分:0)

这应该有效:

$data_to_save['title'] = (string) $picasa_feed->title;