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

I'm trying to parse the most recent 3 news articles in an RSS feed. After that, it should create a "preview" of the description, and then display the title and the preview. I got it to display the first article 3 times...

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

I also got it to "work" (show the first 3) by using a foreach loop...

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

It's commented out because it makes less sense to me.

Please help! Thanks!

Answers


You have pushed the rss items into the $arrFeeds and now you can access these by the index, e.g. $arrFeeds[0] would be the first rss item.

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

But the following is better: with 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 />");
}

When you use foreach, there are there are two (in your case, but there can be 3) possible "parameters" that it accepts. An array to traverse and a variable to store each element. This example:

$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"

The iterator never modifies the array. It just sets $number to be the value of each element in the array until there are no more elements. The same principle applies to a for loop. The code below 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]);
}

The same applies when you use multidimensional arrays. Whether you choose for or foreach, you have to access each element in the array. So your code will look something like this:

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

I chose to use foreach because I find it's much easier to let PHP handle the iteration. Plus, the foreach line reads properly in English. For each item in $rssItems alias it as $rssItem. Notice the difference between the plural and the singular. The foreach runs its block for each item in $rssItems and for each iteration the variable $rssItem will contain the current value in $rssItems that the loop is on.

Want more of a reason to use foreach? If you need to work with the key too, foreach will give you both the key and value of every element of the hash (array).

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

Foreach also allows you with SPL to write classes that extend or implement an iterator. Classes that contain collections, can function as a class but can be iterated over. For example:

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

Need Your Help

How to enumerate through dictionary in Objective-C in the order in which they were defined

objective-c nsdictionary enumeration

Is it possible to enumerate NSDictionary in the order in which the key-value is defined?

New Keywords in Java 9

java syntax java-9 jigsaw

One of Java 9's largest features will be a module system defined by Project Jigsaw. When reading slides from the Project Jigsaw: Under the Hood at JavaOne 2015, I noticed the following source code:...