Stripping last comma from a foreach loop in PHP?

PHPServer Side ProgrammingProgramming

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
raja
Published on 06-Apr-2020 09:09:20
Advertisements