dongzhunnai0140 2014-03-07 03:05
浏览 36
已采纳

foreach工作,关键不,PHP

I know this is probably a misunderstanding in syntax and I'm sure it is because I am working with Java and C++ right now on top of PHP, but nonetheless I would really appreciate some guidance. This is in PHP.

This works,

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

This does not,

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

Appreciate the help.

As per, "zerkms" remarks,

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)
}
  • 写回答

1条回答 默认 最新

  • dongyipa0028 2014-03-07 03:23
    关注

    As soon as it's a Zend_Feed_Rss instance and as soon as you want to get the first item's title you may do it using:

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

    $this->channel is not array (and doesn't implement ArrayAccess interface) - so you cannot use [] to retrieve it's items.

    But it implements an iterator interface, so you can use current() (which presumably initially points to the first element).

    PS: OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOPS. It does implement ArrayAccess so $this->channel[0] should work.

    What would var_dump($this->channel[0]); output?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b