在PHP中访问多维数组中的信息时遇到问题

在PHP中访问多维数组中的信息时遇到问题,php,arrays,multidimensional-array,rss,Php,Arrays,Multidimensional Array,Rss,我试图在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=16c4c7582db87da06664

我试图在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 />");
}
*/
/*
foreach($itemrs作为$ira)
{
$title=$itemrs['title'];
$description=substr($itemRSS['description'],0100);
echo(“$title.”);
回声(“
.”$description.“
”); } */
它被注释掉了,因为它对我来说没什么意义


请帮忙!谢谢

您已将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时,有两个(在您的例子中,但可能有3个)它接受的可能“参数”。要遍历的数组和存储每个元素的变量。这个例子:

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

迭代器从不修改数组。它只是将
$number
设置为数组中每个元素的值,直到没有更多元素为止。同样的原则也适用于
for
循环。下面是代码

我选择使用foreach是因为我发现让PHP处理迭代要容易得多。另外,
foreach
行正确地读取英语。对于
$rssItems
中的每个项目,将其别名为
$rssItem
。注意复数和单数之间的区别。
foreach
$rssItems
中的每个项目运行其块,对于每个迭代,变量
$rssItems
将包含循环启用的
$rssItems
中的当前值

想要更多的理由来使用
foreach
?如果您也需要使用键,foreach将为您提供哈希(数组)的每个元素的键和值

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

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

您需要处理$itemRSS中的索引。因此,尝试一些类似$title=$itemrs[$i]['title']的方法;这会破坏整个页面。。。我现在有了“foreach”,您可以在这里看到结果:代码应该显示在“我们的博客”下面,但它将我们的页脚垂直放置,并且。。。
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"
$numbers = array('one', 'two');
for($i = 0; $i < count($numbers); $i++) {
    echo "\nNew Iteration\n";
    var_dump($numbers);
    var_dump($numbers[$i]);
}
foreach($rssItems as $rssItem)
{
    $title = $rssItem['title'];
    $description = substr($rssItem['description'],0,100);
    echo("<h2>".$title."</h2>");
    echo("<br />".$description."<br />");
}
$words = array('hola' => 'hello', 'adios' => 'goodbye', 'gracias' => 'thank you');
foreach($words as $spanishWord => $englishTranslation) {
    echo $spanishWord . " in English is " . $englishTranslation . "\n";
}
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;
}