Morteza
Morteza

Reputation: 2153

php foreach : each link in separate variable

I get some links from feed with this code on simplepie :

 if ($check) :
  foreach ($feed->get_items(0,3) as $item):
  $links = $item->get_permalink();
  echo $links;
 endforeach; endif;

thats result me:

http://link1....
http://link2....
http://link3....

i want to put each link in separate variable like :

$links1 = 'http://link1....';
$links1 = 'http://link2....';
$links1 = 'http://link3....';

thanks , mori

Upvotes: 0

Views: 359

Answers (3)

praneeth
praneeth

Reputation: 535

if ($check) :
$i=0;
  foreach ($feed->get_items(0,3) as $item):
  $links.$i = $item->get_permalink();
  echo $links.$i;
$i++;
 endforeach; endif;

I think it may works...

Upvotes: 0

ariefbayu
ariefbayu

Reputation: 21979

Maybe what you want is array variable?

try this:

 if ($check) :
   foreach ($feed->get_items(0,3) as $item):
     $links[] = $item->get_permalink();
 endforeach; endif;

Upvotes: 3

ThiefMaster
ThiefMaster

Reputation: 318518

$i = 0;
foreach($feed->get_items(0,3) as $item)) {
    ${'link' . ++$i} = $item->get_permalink();
}

Upvotes: 3

Related Questions