
- 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
How to create comma separated list from an array in PHP?
To create a comma-separated list from an array in PHP, the code is as follows−
Example
<?php $arr = array( " John ", "Jacob ", " Tom ", " Tim "); echo "Array with leading and trailing whitespaces...
"; foreach( $arr as $value ) { echo "Value = $value
"; } echo "
Comma separated list...
"; print_r(implode(', ', $arr)); $result = array_map('trim', $arr); echo "
Updated Array...
"; foreach( $result as $value ) { echo "Value = $value
"; } ?>
Output
This will produce the following output−
Array with leading and trailing whitespaces... Value = John Value = Jacob Value = Tom Value = Tim Comma separated list... John , Jacob , Tom , Tim Updated Array... Value = John Value = Jacob Value = Tom Value = Tim
Example
Let us now see another example −
<?php $arr = array(100, 200, 300, 400, 500); echo "Comma separated list...
"; print_r(implode(', ', $arr)); ?>
Output
This will produce the following output−
Comma separated list... 100, 200, 300, 400, 500
- Related Articles
- How to create a comma separated string from a list of string in C#?
- How to turn JavaScript array into the comma-separated list?
- MySQL query to fetch specific records matched from an array (comma separated values)
- How would you make a comma-separated string from a list in Python?
- How to convert array of comma separated strings to array of objects?
- How to split comma separated values in an R vector?
- Convert String into comma separated List in Java
- How to set a comma separated list as a table in MySQL?
- Display MySQL Results as comma separated list?
- MySQL query to retrieve records from the part of a comma-separated list?
- How to use a comma-separated string in an `IN ()` in MySQL?
- Count values from comma-separated field in MySQL?
- How to display data from a MySQL column separated by comma?
- How to convert a comma separated String into an ArrayList in Java?
- Python program to convert list of string to comma separated string

Advertisements