在PHP中访问多维数组中的信息时出现问题


Trouble with accessing information in a multi-dimensional array in PHP

我正在尝试解析RSS提要中最近的3篇新闻文章。之后,它应该创建描述的"预览",然后显示标题和预览。我把第一篇文章展示了三次。。。

<?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 />");
}
*/

它被评论掉了,因为它对我来说意义不大。

请帮忙!谢谢

您已经将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 />");
}

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

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

将输出:

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循环。下面的代码将产生与上面相同的结果。

$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;
}