【问题标题】:foreach results in an array, how can I get the value of element instead of an array?foreach 生成一个数组,如何获取元素的值而不是数组?
【发布时间】:2016-01-19 06:57:21
【问题描述】:

我是一个不熟练的 PHP 用户,正在尝试生成 XML RSS 文档,
使用 PHP 类 RSS_PHP RSS Parser

foreach 返回 30 <items> 这很好,因为这是 xml 源中的项目数,但是当尝试获取 <title> 的值时,结果是 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>

PHP Class网站提供的例子是:

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

?>

【问题讨论】:

  • 那么,title 来自哪里?
  • 创建后不附加到根元素
  • @charlietfl 谢谢你.. 很抱歉问你,你能给我更多关于如何附加到根文档的信息吗?谢谢。
  • 所以如果 &lt;channel&gt; 是根元素...而不是 $xml-&gt;addChild 之后做 $channel-&gt;addChild

标签: javascript php xml rss


【解决方案1】:

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

【讨论】:

  • 非常感谢您的帮助、解释和解决方案代码,它有效,您不知道您对我的帮助有多大,谢谢!
【解决方案2】:

基于RSS parser 中的示例,每个$item 都是一个关联数组,其中包含titledescription 等键。 ['title'] 是一个包含一个字符串 'title' 的数组文字,如果您想访问某个项目的标题,您可以使用 $item['title'] 给您类似的内容:

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

【讨论】:

  • 非常感谢@mzulch 我替换了' $title = $item->addChild('title', ''.['title'].''); //为'添加标题节点' $title = $item->addChild('title', $item['title']); //add title node ' 数组被移除,但结果为空 '' 你可以在我的网站上看到它:[link] zstreambox.com/mkttools/rssphp/t1.php [link]
猜你喜欢
  • 2018-10-16
  • 2017-05-31
  • 1970-01-01
  • 2018-05-29
  • 2011-05-04
  • 2019-06-01
  • 2016-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多