View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
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.
Output:
Array [ [0] => GFG1 [1] => GFG2 [2] => GFG3 ] GFG1, GFG2, GFG3
Example 2:
Output:
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
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
explode
has some very big problems in real life usage:
count[explode[',', null]]; // 1 !!
explode[',', null]; // [""] not an empty array, but an array with one empty string!
explode[',', ""]; // [""]
explode[',', "1,"]; // ["1",""] ending commas are also unsupported, kinda like IE8
this is why i prefer preg_split
preg_split['@,@', $string, NULL, PREG_SPLIT_NO_EMPTY]
the entire boilerplate:
/** @brief wrapper for explode
* @param string|int|array $val string will explode. '' return []. int return string in array [1 returns ['1']]. array return itself. for other types - see $as_is
* @param bool $as_is false [default]: bool/null return []. true: bool/null return itself.
* @param string $delimiter default ','
* @return array|mixed
*/
public static function explode[$val, $as_is = false, $delimiter = ',']
{
// using preg_split [instead of explode] because it is the best way to handle ending comma and avoid empty string converted to ['']
return [is_string[$val] || is_int[$val]] ?
preg_split['@' . preg_quote[$delimiter, '@'] . '@', $val, NULL, PREG_SPLIT_NO_EMPTY]
:
[$as_is ? $val : [is_array[$val] ? $val : []]];
}
To split a given comma delimited string into an array of values, the PHP code is as follows −
Example
Live Demo
Output
The array is Array [ [0] => 456 [1] => 789 [2] => 23 [3] => 4 [4] => 019 ] The array is Array [ [0] => 00 [1] => 876 [2] => 5432 [3] => 1234 [4] => 0 ]
A string of numbers is defined and the ‘preg_split’ function is used to separate the numbers based on the occurance of ‘/’ or ‘.’. The split array is now printed. Another method to split the given number in the form of a string is to use the explode function. It splits the array based on the occurance of a comma.
Updated on 02-Jul-2020 06:54:26
- Related Questions & Answers
- C# program to convert several strings into a single comma-delimited string
- Parsing a comma-delimited std::string in C++
- How to split a String into an array in Kotlin?
- MySQL select distinct rows into a comma delimited list column?
- Combining multiple rows into a comma delimited list in MySQL?
- How to split comma and semicolon separated string into a two-dimensional array in JavaScript ?
- Python program to split a string and join with comma
- PHP Pushing values into an associative array?
- PHP preg_split to split a string with specific values?
- How to split comma separated values in an R vector?
- How to split a string into elements of a string array in C#?
- How to collapse rows into a comma-delimited list with a single MySQL Query?
- Split string into sentences using regex in PHP
- Split String with Comma [,] in Java
- Program to check whether we can split a string into descending consecutive values in Python