How to get the first item in an Array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

How to get the first item in an Array?

Post by becky-atlanta »

I found this neat script to extract RSS feed:

Code: Select all

<?php
    $doc = new DOMDocument();
    $doc->load('http://betbender.blogspot.com/feeds/posts/default?alt=rss');
    $arrFeeds = array();
    foreach ($doc->getElementsByTagName('item') as $node) {
        $itemRSS = array ( 
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
            );
        array_push($arrFeeds, $itemRSS);
    }
?>
 
I am able to use it to get the feed. Now I am trying to get the first article only. I tried:

Code: Select all

 
    echo $itemRSS["title"];
    echo $itemRSS["desc"];
    echo $itemRSS["link"];
    echo $itemRSS["date"];
 
but I get the last item, not the first one that I want.

I am still very confused with array. Can you give me a hand?
Last edited by Benjamin on Tue May 26, 2009 10:16 am, edited 1 time in total.
Reason: Changed code type from text to php.
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: How to get the first item in an Array?

Post by divito »

Code: Select all

<?php
    $doc = new DOMDocument();
    $doc->load('http://betbender.blogspot.com/feeds/posts/default?alt=rss');
    $arrFeeds = array();
    foreach ($doc->getElementsByTagName('item') as $node) {
        $itemRSS = array (
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
            );
        array_push($arrFeeds, $itemRSS);
    }
    krsort($arrFeeds);
?>
 
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: How to get the first item in an Array?

Post by becky-atlanta »

Thanks for your help. However, it is not working. It is still giving me the last item.
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: How to get the first item in an Array?

Post by divito »

Can we see the next bit of code after the sort?
Scriptor
Forum Newbie
Posts: 9
Joined: Fri May 22, 2009 12:27 am

Re: How to get the first item in an Array?

Post by Scriptor »

Code: Select all

reset($array);
that will show first item of the array.

or if you want just 1 single array you can always just pull it out...

Code: Select all

echo $array[0];
Last edited by Benjamin on Tue May 26, 2009 10:16 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: How to get the first item in an Array?

Post by becky-atlanta »

divito wrote:Can we see the next bit of code after the sort?
After the reverse sort, I just simply used the code I was using to test:

Code: Select all

 
echo $itemRSS["title"];
echo $itemRSS["desc"];
echo $itemRSS["link"];
echo $itemRSS["date"];
 
and it returned the same item I was getting before the reverse sort.
Scriptor wrote:echo $array[0];
I tried:

Code: Select all

echo $arrFeeds[0];  \* it output "Array".
I tried:

Code: Select all

echo $itemRSS[0];  \* it output nothing.
Last edited by Benjamin on Tue May 26, 2009 10:17 am, edited 1 time in total.
Reason: Added [code=php] tags.
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: How to get the first item in an Array?

Post by divito »

Try this:

Code: Select all

<?php
    $doc = new DOMDocument();
    $doc->load('http://betbender.blogspot.com/feeds/posts/default?alt=rss');
    $arrFeeds = array();
    foreach ($doc->getElementsByTagName('item') as $node) {
        $itemRSS = array (
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
            );
        array_push($arrFeeds, $itemRSS);
    }
    krsort($arrFeeds);
 
    foreach ($arrFeeds as $entry) {
       
       echo $entry["title"];
       echo $entry["desc"];
       echo $entry["link"];
       echo $entry["date"];
}
?>
If it works, just throw in a counter and you should be set, assuming that helps in the way you're deploying this.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to get the first item in an Array?

Post by requinix »

Like Scriptor said three hours ago, the first item in an array is

Code: Select all

$first = reset($array);
If you want the first chronologically then it'll be the last item the code processed (since RSS lists newest -> oldest).

Code: Select all

$first = $itemRSS;
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: How to get the first item in an Array?

Post by becky-atlanta »

Thank you for everyone's tips. Now I learned some new codes I did not know before. :D

Here's how I make it work:

Code: Select all

<?php
    $doc = new DOMDocument();
    $doc->load('http://betbender.blogspot.com/feeds/posts/default?alt=rss');
    $arrFeeds = array();
    $i = 0;                [color=#FF0000] //added to get the first item only//[/color]
    foreach ($doc->getElementsByTagName('item') as $node) {
        if (++$i == 1){     [color=#FF0000]//added to get the first item only//[/color]
        $itemRSS = array ( 
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
            );
        array_push($arrFeeds, $itemRSS);
        echo "<a href=\"";
        echo $itemRSS["link"];
        echo "\"><h1>";
        echo $itemRSS["title"]; 
        echo "</h1></a>";
        echo "<br>";
        echo $itemRSS["desc"];
        echo "<br>";
        echo $itemRSS["date"];
        echo "<br>";
        break;}             [color=#FF0000]//added to get the first item only//[/color]
    }
?>
 
Here is a good page to explain different ways to get the first element of an array,
http://slavi.biz/blog/how-to-get-the-fi ... 01-10.html
Last edited by Benjamin on Tue May 26, 2009 10:18 am, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply