Jump to content

foreach loop - how to not loop witha "," on last loop


denla

Recommended Posts

Hey guys,

 

Got another question im hoping someone can help me with.

 

I have a foreach loop (for use in a mysql query):

 

foreach ($interests as $interest) {

    $query .= "($id, $interest), ";

}

 

problem is i do not want the comma(,) in the last loop.

 

Is there some kinda of function i can use so it does not insert it on last loop?

 

Or should i just use a for loop with a nested if loop? something like ;

 

for($i=0; $i < count($interests); $i++){

$query .=

"($id, '$interests[$i]')";

if($i + 1 < count($interests)) {

$query .= ", ";

}

}

 

 

Cheers guys

 

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.