2012-07-17 101 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; 
}