foreach 生成一个数组,如何获取元素的值而不是数组?

foreach results in an array, how can I get the value of element instead of an array?

我是一个外行 PHP 用户试图生成 XML RSS 文档,
使用 PHP class RSS_PHP RSS Parser

foreach returns 30 <items> 这很好,因为那是 xml 源上的项目数,但是在尝试获取 xml 的值时=16=] 结果是 Array

我不知道我遗漏了什么或需要更改什么才能从 source 而不是数组中获取 <title> 的值。

非常感谢任何帮助,谢谢。

<?php 
require_once 'rss_php.php';
header('Content-Type: text/xml; charset=utf-8', true); //set document header content type to be XML

 $xml = new SimpleXMLElement('<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom"></rss>');
 $xml->addAttribute('version', '2.0');

 $rss = new rss_php;
    $rss->load('http://rss.tvguide.com/breakingnews');
    $items = $rss->getItems();
 
 $channel = $xml->addChild('channel'); //add channel node
 $title = $channel->addChild('title','Channel Title Here'); //title of the feed
 $link = $channel->addChild('link','http://www.zstreambox.com'); //feed site
 $description = $channel->addChild('description','Channel description here'); //feed description
 $language = $channel->addChild('language','en-us'); //language
 $ttl = $channel->addChild('ttl','120'); //time
 $image = $channel->addChild('image'); //add atom node
 $url = $image->addChild('url','http://static.tvgcdn.net/www/img/tvguide-feed-logo.png'); //add channel image
 $title = $image->addChild('title','ZSB: Breaking News'); //add channel title
 $link = $image->addChild('link','http://www.zstreambox.com'); //add channel link


 foreach($items as $index => $item) {  
  $item = $channel->addChild('item'); //add item node
  $title = $item->addChild('title', $item['title']); //add title node

}

 echo $xml->asXML();

?>

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Channel Title Here</title>
<link>http://www.zstreambox.com</link>
<description>Channel description here</description>
<language>en-us</language>
<ttl>120</ttl>
<image>...</image>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
</channel>
</rss>

PHPClass网站提供的例子是:

<?php
require_once 'rss_php.php';    

    $rss = new rss_php;
    $rss->load('http://digg.com/rss/index.xml');
    $items = $rss->getItems();

    $html = '';
    foreach($items as $index => $item) {
        $html .= '<p><a href="'.$item['link'].'" title="'.$item['title'].'"><strong>'.$item['title'].'</strong></a><br />
                    '.$item['description'].'<br />
                    Submitted by: <a href="'.$item['digg:submitter']['digg:userimage'].'">'.$item['digg:submitter']['digg:username'].'</a> :: '.$item['pubDate'].'<br />
                    Diggs: '.$item['digg:diggCount'].' :: <em>Category: '.$item['digg:category'].'</em>
                    </p>';
    }
    
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Digg Front Page</title>
</head>

<body>
<?php
echo $html;
?>
</body>
</html>

我只想包含更正后的工作代码以备不时之需 感谢在这个很棒的网站上分享和帮助的了不起的人

<?php 
require_once 'rss_php.php';
header('Content-Type: text/xml; charset=utf-8', true); //set document header content type to be XML

 $xml = new SimpleXMLElement('<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom"></rss>');
 $xml->addAttribute('version', '2.0');

 $rss = new rss_php;
    $rss->load('http://rss.tvguide.com/breakingnews');
    $items = $rss->getItems();
 
 $channel = $xml->addChild('channel'); //add channel node
 $title = $channel->addChild('title','Channel Title Here'); //title of the feed
 $link = $channel->addChild('link','http://www.zstreambox.com'); //feed site
 $description = $channel->addChild('description','Channel description here'); //feed description
 $language = $channel->addChild('language','en-us'); //language
 $ttl = $channel->addChild('ttl','120'); //time
 $image = $channel->addChild('image'); //add atom node
 $url = $image->addChild('url','http://static.tvgcdn.net/www/img/tvguide-feed-logo.png'); //add channel image
 $title = $image->addChild('title','ZSB: Breaking News'); //add channel title
 $link = $image->addChild('link','http://www.zstreambox.com'); //add channel link


 foreach($items as $index => $item) {        
     $xml_item = $channel->addChild('item'); //add item node
     $title = $xml_item->addChild('title', ''.$item['title'].''); 
     $description = $xml_item->addChild('description', ''. $item['description'] . '');
 }

 echo $xml->asXML();

?>

基于 RSS parser 中的示例,每个 $item 都是一个关联数组,其中包含 titledescription 等键。 ['title'] 是一个包含一个字符串的数组文字,'title',如果你想访问一个项目的标题,你会使用 $item['title'] 给你这样的东西:

$title = $item->addChild('title', $item['title']); //add title node 

您了解 foreach 中的 $item 变量发生了什么吗?

foreach($items as $index => $item) {        
    // here $item in a item of RSS feed
    $item = $xml->addChild('item'); //add item node
    // and here $item is an XML-node
    // and as it is an XML-node now - 
    // it doesn't have `title` or `description` property
    $title = $item->addChild('title', ''.['title'].''); //add title node    
    $description = $item->addChild('description', ''. ['description'] . ''); //add description
}

正确代码:

foreach($items as $index => $item) {        
    $xml_item = $xml->addChild('item'); //add item node
    $title = $xml_item->addChild('title', ''.$item['title'].''); 
    $description = $xml_item->addChild('description', ''. $item['description'] . '');
}