Strip Last Comma from a Foreach Loop in PHP



Below is the code that can be used to strip the last comma from the foreach loop −

Example

 Live Demo

$result_str = array("Hi", "Hello", "have a", "good day");
foreach ($results as $result) {
   $result_str[] = $result->name;
}
echo implode(",",$result_str);

Output

This will produce the following output −

Hi,Hello,have a,good day
Updated on: 2020-04-06T09:09:58+05:30

844 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements