Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.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,can';t使用xml数组(simplexml)_Php_Xml_Arrays_Simplexml - Fatal编程技术网

PHP,can';t使用xml数组(simplexml)

PHP,can';t使用xml数组(simplexml),php,xml,arrays,simplexml,Php,Xml,Arrays,Simplexml,我有一个xml格式的rss,其中包含(格式良好) 这个密码什么也没给我。 如果我var\u dump使用$xml,我将得到40个数组 有人能猜出原因吗 变量转储: object(SimpleXMLElement)#1 (2) { ["@attributes"]=> array(1) { ["version"]=> string(3) "2.0" } ["channel"]=> object(SimpleXMLElement)#2 (6) {

我有一个xml格式的rss,其中包含(格式良好)

这个密码什么也没给我。 如果我
var\u dump
使用
$xml
,我将得到40个数组

有人能猜出原因吗

变量转储:

object(SimpleXMLElement)#1 (2) {
  ["@attributes"]=>
  array(1) {
    ["version"]=>
    string(3) "2.0"
  }
  ["channel"]=>
  object(SimpleXMLElement)#2 (6) {
    ["title"]=>
    string(11) "example.com"
    ["link"]=>
    string(23) "http://www.example.com/"
    ["description"]=>
    string(43) "Example free hosting for free unicorn videos."
    ["lastBuildDate"]=>
    string(31) "Mon, 18 Nov 2013 05:03:00 +0100"
    ["ttl"]=>
    string(2) "20"
    ["item"]=>
    array(40) {
      [0]=>
      object(SimpleXMLElement)#3 (5) {
        ["title"]=>
        string(23) "..."
        ["description"]=>
        string(766) ".. "
        ["link"]=>
        string(59) "http://www.example.com/..."
        ["guid"]=>
        string(12) "video5748963"
        ["pubDate"]=>
        object(SimpleXMLElement)#43 (0) {
        }
      }
      [1]=>
      object(SimpleXMLElement)#4 (5) {
        ["title"]=>
        string(48) "...s"
        ["description"]=>
        string(789) "..."
        ["link"]=>
        string(84) "http://www.example.com/,,,"
        ["guid"]=>
        string(12) "video4863479"
        ["pubDate"]=>
        object(SimpleXMLElement)#43 (0) {
        }
      }
 ....
应该是

$xml->channel->item[1]->guid

这是一个典型RSS文件的结构:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        ...
        <item>...</item>
        <item>...</item>
        ...
    </channel>
</rss>

...
...
...
...
在调用中,必须省略顶级节点(在本例中为
),但不能忽略第二级节点:

echo $xml->item[0]->guid; // <channel>?
echo$xml->item[0]->guid;//?

显示您的转储文件。你的XMLuh听起来不太对劲:喜欢看信息,大部分是NSFW,所以要小心。你应该删除色情链接,这是广告还是真的问题?您正在访问xml->item,但items在xml->channel->item中,可能问题就在这里,正如您在
var_dump
中看到的,您的
item
不是
$xml
的直接子项<代码>echo$xml->channel->item[0]->guid
echo $xml->item[0]->guid; // <channel>?