Php 如何从siplexmlement中提取数据?
我有一个变量$foo,包含以下内容:Php 如何从siplexmlement中提取数据?,php,xml,arrays,xml-parsing,Php,Xml,Arrays,Xml Parsing,我有一个变量$foo,包含以下内容: object(SimpleXMLElement)#969 (2) { ["@attributes"]=> array(1) { ["type"]=> string(4) "html" } [0]=> string(13) "Viernes Santo" } 我正在尝试获取“Viernes Santo”的内容,但我无法获取。我尝试了$foo[0],但返回了相同的内容。有什么想法吗?这可以: (string
object(SimpleXMLElement)#969 (2) {
["@attributes"]=>
array(1) {
["type"]=>
string(4) "html"
}
[0]=>
string(13) "Viernes Santo"
}
我正在尝试获取“Viernes Santo”的内容,但我无法获取。我尝试了$foo[0]
,但返回了相同的内容。有什么想法吗?这可以:
(string) $foo
SimpleXMLElement的行为类似于一个对象,这就是为什么人们会像您一样感到困惑,但它实际上是一种语言构造,有点不同。元素及其数据必须类型转换为您想要使用的数据类型,因为默认情况下,从SimpleXMLElement函数返回的所有内容都是SimpleXMLElement,如果不是由函数进行类型转换,则必须直接进行类型转换 如果你打电话:
echo $foo; //typecast to a string automatically
$myVar = $foo; //SimpleXMLElement
$myVar = (string)$foo; //typecast to a string
只需
echo$foo
并查看PHP手册中的示例