Jump to content
Larry Ullman's Book Forums

Assign Foreach Loop Contents To A Variable


Recommended Posts

Hello forum members,

 

is it possible to assign the contents of a foreach loop to a variable?

 

I am looping out some names. If the foreach loop loops out 5 names, then I'd like all 5 names to be assigned to the $names variable. I think this was working yesterday but perhaps I was imagining things.

$names = '';
foreach($names as $k => $v){
$names .= "<p>$v</p>";
}

echo $names;

Thank you for your help.

 

Link to comment
Share on other sites

HartleySan, thank you for helping me!

 

It's working. I couldn't understand what the problem was until I saw that I had initialized $names after the foreach loop had already begun. But don't tell anyone...

 

Thanks again.

 

Cheers

Link to comment
Share on other sites

 Share

×
×
  • Create New...