How to create comma separated list from an array in PHP ?
The comma separated list can be created by using implode() function. The implode() is a builtin function in PHP and is used to join the elements of an array. implode() is an alias for PHP | join() function and works exactly same as that of join() function.
If we have an array of elements, we can use the implode() function to join them all to form one string. We basically join array elements with a string. Just like join() function , implode() function also returns a string formed from the elements of an array.
Syntax:
string implode( separator, array )
Return Type: The return type of implode() function is string. It will return the joined string formed from the elements of array.
Example 1: This example adds comma separator to the array elements.
<?php // Declare an array and initialize it $Array = array( "GFG1", "GFG2", "GFG3" ); // Display the array elements print_r($Array); // Use implode() function to join // comma in the array $List = implode(', ', $Array); // Display the comma separated list print_r($List); ?> |
Array
(
[0] => GFG1
[1] => GFG2
[2] => GFG3
)
GFG1, GFG2, GFG3
Example 2:
<?php // Declare an array and initialize it $Array = array(0, 1, 2, 3, 4, 5, 6, 7); // Display the array elements print_r($Array); // Use implode() function to join // comma in the array $List = implode(', ', $Array); // Display the comma separated list print_r($List); ?> |
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
)
0, 1, 2, 3, 4, 5, 6, 7
Recommended Posts:
- Create a comma separated list from an array in JavaScript
- Convert comma separated string to array using JavaScript
- Split a comma delimited string into an array in PHP
- Create an unordered list without any bullets using CSS
- How to create an array for JSON using PHP?
- How to create two dimensional array in JavaScript?
- Create a Pandas Series from array
- How to get a list of associative array keys in JavaScript ?
- How to convert list of elements in an array using jQuery ?
- How to create a div element in jQuery ?
- How to create a style tag using JavaScript?
- jQuery | Create a div element
- How to create Radial Menu in CSS ?
- How to create an unordered_map of pairs in C++?
- Different ways to create an Object in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



