对于每个结果到一个数组,我怎样才能得到元素的值而不是数组


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

我是一个不专业的PHP用户,试图生成一个XML RSS文档,
使用 PHP 类RSS_PHP RSS 解析器

foreach返回 30 <items>这很好,因为这是 xml 源上的项目数,但是当尝试获取 <title> 结果的值时,它是一个Array

我不知道我错过了什么,或者我需要更改什么才能从源而不是数组中获取<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>

PHP 类网站提供的示例是:

<?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();
?>

您了解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'] . '');
}

根据 RSS 解析器的示例,每个$item都是一个具有 titledescription 等键的关联数组。 ['title'] 是一个包含字符串的数组文本,'title' ,如果要访问项目的标题,可以使用$item['title']为您提供如下内容:

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