Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从siplexmlement中提取数据?_Php_Xml_Arrays_Xml Parsing - Fatal编程技术网

Php 如何从siplexmlement中提取数据?

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

我有一个变量$foo,包含以下内容:

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手册中的示例