2012-07-17 105 views
0

我试图解析RSS提要中最新的3篇新闻文章。之后,它应该创建一个描述的“预览”,然后显示标题和预览。我把它显示在第一篇文章的3倍......在PHP中访问多维数组中的信息时遇到问题

<?php 
$doc = new DOMDocument(); 
$doc->load('http://info.arkmediainc.com/CMS/UI/Modules/BizBlogger/rss.aspx?tabid=575593&moduleid=1167800&maxcount=25&t=16c4c7582db87da06664437e98a74210'); 
$arrFeeds = array(); 
foreach ($doc->getElementsByTagName('item') as $node) { 
    $itemRSS = array ( 
     'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 
     'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 
     'description' => $node->getElementsByTagName('description')->item(0)->nodeValue, 
     'pubDate' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue 
    ); 
    array_push($arrFeeds, $itemRSS); 
} 
$itemRSS = array_slice($itemRSS, 0, 3); // This cuts it down to 3 articles. 


for ($i = 0; $i < 3; $i++) 
{ 
    $title = $itemRSS['title']; 
    $description = substr($itemRSS['description'],0,100); 
    echo("<h2>".$title."</h2>"); 
    echo("<br />".$description."<br />"); 
} 
?> 

我也得到了它的“工作”通过使用foreach循环(显示第3)...

/* 
foreach($itemRSS as $ira) 
{ 
    $title = $itemRSS['title']; 
    $description = substr($itemRSS['description'],0,100); 
    echo("<h2>".$title."</h2>"); 
    echo("<br />".$description."<br />"); 
} 
*/ 

这是注释掉了,因为它对我来说意义不大。

请帮忙!谢谢!

+0

您需要处理$ itemRSS中的索引。所以试试像$ title = $ itemRSS [$ i] ['title']; – tubaguy50035 2012-07-17 21:46:59

+0

不起作用... – 2012-07-17 22:30:26

回答

0

您已将rss项目推入$arrFeeds,现在您可以通过索引访问这些项目,例如, $arrFeeds[0]将是第一个rss项目。

for ($i = 0; $i < 3; $i++) 
{ 
    $title = $arrFeeds[$i]['title']; 
    $description = substr($arrFeeds[$i]['description'],0,100); 
    echo("<h2>".$title."</h2>"); 
    echo("<br />".$description."<br />"); 
} 

但下面是更好:foreach

$theFirstThreeArticles = array_slice($arrFeeds, 0, 3); // This cuts it down to 3 articles. 

foreach($theFirstThreeArticles as $ira) 
{ 
    $title = $ira['title']; 
    $description = substr($ira['description'],0,100); 
    echo("<h2>".$title."</h2>"); 
    echo("<br />".$description."<br />"); 
} 
+0

打破了整个页面...我现在有了“foreach”,你可以在这里看到结果: http://www.arkmediainc.com/ark20test/ark20test /index.php 该代码应该出现在“我们的博客”下面,但它将页脚垂直放置并且... – 2012-07-17 22:07:43

+0

我看到它正在工作 – Besnik 2012-07-18 07:32:26

0

当您使用的foreach,有有两个(在你的情况,但可以有3)可能的 “参数”它接受。一个数组遍历和一个变量来存储每个元素。这个例子:

$numbers = array('one', 'two'); 
foreach($numbers as $number) { 
    echo "\nNew Iteration\n"; 
    var_dump($numbers); 
    var_dump($number); 
} 

Will output

New Iteration 
array(2) { 
    [0] => string(3) "one" 
    [1] => string(3) "two" 
} 
string(3) "one" 

New Iteration 
array(2) { 
    [0] => string(3) "one" 
    [1] => string(3) "two" 
} 
string(3) "two" 

迭代器从未修改阵列。它只是将$number设置为数组中每个元素的值,直到没有更多元素为止。相同的原则适用于for循环。代码在will produce the same as above以下。

$numbers = array('one', 'two'); 
for($i = 0; $i < count($numbers); $i++) { 
    echo "\nNew Iteration\n"; 
    var_dump($numbers); 
    var_dump($numbers[$i]); 
} 

这同样适用于使用多维数组的情况。无论您选择for还是foreach,都必须访问阵列中的每个元素。所以,你的代码将是这个样子:

foreach($rssItems as $rssItem) 
{ 
    $title = $rssItem['title']; 
    $description = substr($rssItem['description'],0,100); 
    echo("<h2>".$title."</h2>"); 
    echo("<br />".$description."<br />"); 
} 

我选择使用的foreach,因为我觉得它更容易让PHP处理迭代。另外,foreach行可正确读取英文。对于$rssItems中的每个项目,将其别名为$rssItem。注意复数与单数的区别。 foreach$rssItems中的每个项目运行其块,并且对于每次迭代,变量$rssItem将包含循环打开的$rssItems中的当前值。

想要更多的原因使用foreach?如果您还需要使用该键,则foreach将为您提供散列(数组)的每个元素的键和值。

$words = array('hola' => 'hello', 'adios' => 'goodbye', 'gracias' => 'thank you'); 
foreach($words as $spanishWord => $englishTranslation) { 
    echo $spanishWord . " in English is " . $englishTranslation . "\n"; 
} 

Foreach还允许您用SPL编写扩展或实现迭代器的类。包含集合的类可以用作类,但可以迭代。例如:

class PhotoAlbum implements IteratorAggregate { 
    private $pictures = array(); 

    public function __construct($pictures) { 
     $this->pictures = $pictures; 
    } 

    public function addPicture($picture) { 
     $this->pictures[] = $picture; 
    } 

    public function share($friend) { 
     // etc.. 
    } 

    public function getIterator() { 
     return new ArrayIterator($this->pictures); 
    } 
} 

$album = new PhotoAlbum($picturesArrayFromDB); 
foreach($album as $picture) { 
    echo $picture; 
}