【问题标题】:foreach works, key does not, PHPforeach 有效,key 无效,PHP
【发布时间】:2014-04-10 01:00:31
【问题描述】:

我知道这可能是对语法的误解,我确信这是因为我现在在 PHP 之上使用 Java 和 C++,但我仍然非常感谢一些指导。这是在 PHP 中。

这行得通,

$entry;
foreach($this->channel as $i){
    $entry = $i;
    break;
}
echo $entry->title();

这不是,

echo $this->channel[0]->title();

感谢您的帮助。

按照“zerkms”的说法,

object(Zend_Feed_Rss)#43 (9) {
  ["_entryClassName":protected] => string(19) "Zend_Feed_Entry_Rss"
  ["_entryElementName":protected] => string(4) "item"
  ["_defaultNamespace":protected] => string(3) "rss"
  ["_entryIndex":protected] => int(0)
  ["_entries":protected] => array(1) {
    [0] => object(DOMElement)#63 (17) {
      ["tagName"] => string(4) "item"
      ["schemaTypeInfo"] => NULL
      ["nodeName"] => string(4) "item"
      ["nodeValue"] => string(1058) "tag:blogger.com,1999:blog-7914458791192620477.post-6083787759488123555Fri, 07 Mar 2014 02:03:00 +00002014-03-06T18:03:27.296-08:00Test Title: tamuHack<strong style="font-family: Arial, Helvetica, sans; font-size: 11px; line-height: 14px; text-align: justify;">Lorem Ipsum</strong><span style="font-family: Arial, Helvetica, sans; font-size: 11px; line-height: 14px; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span>http://tamuhack.blogspot.com/2014/03/test-title-tamuhack.htmlnoreply@blogger.com (Robert Timm)0"
      ["nodeType"] => int(1)
      ["parentNode"] => string(22) "(object value omitted)"
      ["childNodes"] => string(22) "(object value omitted)"
      ["firstChild"] => string(22) "(object value omitted)"
      ["lastChild"] => string(22) "(object value omitted)"
      ["previousSibling"] => string(22) "(object value omitted)"
      ["attributes"] => string(22) "(object value omitted)"
      ["ownerDocument"] => string(22) "(object value omitted)"
      ["namespaceURI"] => NULL
      ["prefix"] => string(0) ""
      ["localName"] => string(4) "item"
      ["baseURI"] => string(44) "/Users/bob/School/tamu_hack/src/public_html/"
      ["textContent"] => string(1058) "tag:blogger.com,1999:blog-7914458791192620477.post-6083787759488123555Fri, 07 Mar 2014 02:03:00 +00002014-03-06T18:03:27.296-08:00Test Title: tamuHack<strong style="font-family: Arial, Helvetica, sans; font-size: 11px; line-height: 14px; text-align: justify;">Lorem Ipsum</strong><span style="font-family: Arial, Helvetica, sans; font-size: 11px; line-height: 14px; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span>http://tamuhack.blogspot.com/2014/03/test-title-tamuhack.htmlnoreply@blogger.com (Robert Timm)0"
    }
  }
  ["_element":protected] => object(DOMElement)#62 (17) {
    ["tagName"] => string(7) "channel"
    ["schemaTypeInfo"] => NULL
    ["nodeName"] => string(7) "channel"
    ["nodeValue"] => string(1215) "tag:blogger.com,1999:blog-7914458791192620477Fri, 07 Mar 2014 02:03:27 +0000tamuHackhttp://tamuhack.blogspot.com/noreply@blogger.com (Robert Timm)Blogger1125tag:blogger.com,1999:blog-7914458791192620477.post-6083787759488123555Fri, 07 Mar 2014 02:03:00 +00002014-03-06T18:03:27.296-08:00Test Title: tamuHack<strong style="font-family: Arial, Helvetica, sans; font-size: 11px; line-height: 14px; text-align: justify;">Lorem Ipsum</strong><span style="font-family: Arial, Helvetica, sans; font-size: 11px; line-height: 14px; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span>http://tamuhack.blogspot.com/2014/03/test-title-tamuhack.htmlnoreply@blogger.com (Robert Timm)0"
    ["nodeType"] => int(1)
    ["parentNode"] => string(22) "(object value omitted)"
    ["childNodes"] => string(22) "(object value omitted)"
    ["firstChild"] => string(22) "(object value omitted)"
    ["lastChild"] => string(22) "(object value omitted)"
    ["previousSibling"] => NULL
    ["attributes"] => string(22) "(object value omitted)"
    ["ownerDocument"] => string(22) "(object value omitted)"
    ["namespaceURI"] => NULL
    ["prefix"] => string(0) ""
    ["localName"] => string(7) "channel"
    ["baseURI"] => string(44) "/Users/bob/School/tamu_hack/src/public_html/"
    ["textContent"] => string(1215) "tag:blogger.com,1999:blog-7914458791192620477Fri, 07 Mar 2014 02:03:27 +0000tamuHackhttp://tamuhack.blogspot.com/noreply@blogger.com (Robert Timm)Blogger1125tag:blogger.com,1999:blog-7914458791192620477.post-6083787759488123555Fri, 07 Mar 2014 02:03:00 +00002014-03-06T18:03:27.296-08:00Test Title: tamuHack<strong style="font-family: Arial, Helvetica, sans; font-size: 11px; line-height: 14px; text-align: justify;">Lorem Ipsum</strong><span style="font-family: Arial, Helvetica, sans; font-size: 11px; line-height: 14px; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span>http://tamuhack.blogspot.com/2014/03/test-title-tamuhack.htmlnoreply@blogger.com (Robert Timm)0"
  }
  ["_encoding":protected] => string(5) "UTF-8"
  ["_parentElement":protected] => NULL
  ["_appended":protected] => bool(true)
}

【问题讨论】:

  • "foreach 有效,key 无效,PHP" --- 这是俳句
  • 现在认真:var_dump($this-&gt;channel); - 检查结构,它不一定是索引数组 PS:$entry;
  • 编辑了变量“channel”的调试。另外,您介意在“这没有任何意义”方面更清楚一点吗? @zerkms?我假设在这种情况下有一个变量范围,因此它的声明。
  • 在 php 中没有像在 java 和 c++ 中那样的变量定义。您只能使用该值对其进行初始化。
  • 这是有道理的。感谢您的澄清。

标签: php foreach indexing


【解决方案1】:

只要它是 Zend_Feed_Rss 实例,并且一旦您想获得第一项的标题,您就可以使用:

$entry = $this->channel->current();

$this-&gt;channel 不是数组(也没有实现ArrayAccess 接口) - 所以你不能使用[] 来检索它的项目。

但是它实现了一个迭代器接口,所以你可以使用current()(大概最初指向第一个元素)。

PS:噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢!它确实实现了ArrayAccess,所以$this-&gt;channel[0] 应该可以工作。

var_dump($this-&gt;channel[0]); 会输出什么?

【讨论】:

  • var_dump($this->channel[0]);,输出 "string(0) """
  • @BajaBob:这很奇怪。从界面的角度来看完全出乎意料。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
  • 2017-07-16
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
  • 2015-02-18
相关资源
最近更新 更多