Page 1 of 1
How to get the first item in an Array?
Posted: Sat May 23, 2009 9:28 am
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?
Re: How to get the first item in an Array?
Posted: Sat May 23, 2009 10:26 am
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);
?>
Re: How to get the first item in an Array?
Posted: Sat May 23, 2009 11:38 am
by becky-atlanta
Thanks for your help. However, it is not working. It is still giving me the last item.
Re: How to get the first item in an Array?
Posted: Sat May 23, 2009 1:24 pm
by divito
Can we see the next bit of code after the sort?
Re: How to get the first item in an Array?
Posted: Sat May 23, 2009 3:01 pm
by Scriptor
that will show first item of the array.
or if you want just 1 single array you can always just pull it out...
Re: How to get the first item in an Array?
Posted: Sat May 23, 2009 5:45 pm
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.
Re: How to get the first item in an Array?
Posted: Sat May 23, 2009 5:57 pm
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.
Re: How to get the first item in an Array?
Posted: Sat May 23, 2009 6:05 pm
by requinix
Like Scriptor said three hours ago, the first item in an array is
If you want the first
chronologically then it'll be the last item the code processed (since RSS lists newest -> oldest).
Re: How to get the first item in an Array?
Posted: Sat May 23, 2009 8:39 pm
by becky-atlanta
Thank you for everyone's tips. Now I learned some new codes I did not know before.
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