PDA

View Full Version : cycling through a php array


oldmanstan
March 10th, 2006, 03:56 AM
I want to go through a php array and if it has multiple items (which it might or might not) I want to place some text in between them. However, I don't want text at the beginning or end.

The only way I can think of to do this is to use a for loop sorta like this:

//print text only between items
for ($i=0; $i < count($array); $i++) {
if($i) {
//add text
}
//print the array value
}

is there a more elegant method? I checked the manual to see if there was a way to check if you were on the first or last iteration of a foreach loop but I couldn't find anything, anybody?

Thanks

dabear
March 10th, 2006, 05:50 AM
Take a look at implode (http://no.php.net/manual/en/function.implode.php)

oldmanstan
March 10th, 2006, 01:07 PM
Well now, that's interesting, thanks!