
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Stripping 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
$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
- Related Articles
- PHP foreach Loop.
- Multiple index variables in PHP foreach loop
- PHP Casting Variable as Object type in foreach Loop
- The internal working of the ‘foreach’ loop in PHP
- How to count values from a PHP array and show value only once in a foreach loop?
- foreach Loop in C#
- Remove comma from a string in PHP?
- Iterating C# StringBuilder in a foreach loop
- Using foreach loop in arrays in C#
- Foreach loop with two arrays and if-condition evaluation to find matching values PHP?
- How does the Java “foreach” loop work?
- How to show a foreach loop using a flow chart in JavaScript?’
- Read last line from file in PHP
- How to use PowerShell break statement in foreach loop?
- How to use PSCustomObject in PowerShell foreach parallel loop?

Advertisements