Jump to content

*Newbie* Displaying 2 Array Items at Once, Then Loop To Get The Rest...


Hilly_2004

Recommended Posts

First off, let me apologise for asking such a newbie question...

 

This is my code....

 


// Fetch RSS feed
$rss = fetch_rss('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/eng_div_3/rss.xml');

if ($rss)
{

// Split the array to show first 10
$items = array_slice($rss->items, 0, 10);

// Cycle through each item and echo
foreach ($items as $item ) 

{
  echo '<li><a href="'.$item['link'].'">'.$item['title'].'</a> - '.$item['description'].'</li>';
}
}
else 
{
  echo '<h2>Error:</h2><p>'.magpie_error().'</p>';
}

 

It's basically displaying information from an RSS feed on my page. However I want to display 2 pieces of that array at once, then in the next loop, get the next 2 arrays, and so on and so forth.

Hi,

 

I would use a for loop instead of a foreach loop as it'll give you a bit more control in this situation. The for loop is based on a counter that'll get incremented by 2 each time round the loop. You can then access array elements $i and $i+1 for the two-at-a-time approach you were after.

 

for ($i = 0; $i < 10; $i += 2)
{
   $item1 = $items[$i];
   $item2 = $items[$i + 1];

   // Do something with $item1 and $item2
}

 

cheers,

Darren.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.