Access SimpleXML node value as string

柔情痞子 提交于 2021-02-08 11:26:30

问题


I have an object called $picasa_feed like this:

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
)

I want to take put a property value into an element of an associative array:

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

When I do that the value of $data_to_save is

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

What I want is

Array
(
    [title] => My Pictures
}

What am I doing wrong and how do I fix it?


回答1:


You want to invoke the SimpleXMLElement's magic __toString method by casting it to string. Try: $data_to_save['title'] = (string)$picasa_feed->title;




回答2:


Cast it to string:

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



回答3:


This should work:

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


来源:https://stackoverflow.com/questions/14862977/access-simplexml-node-value-as-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!