Convert array to comma separated string in PHP; Through this tutorial, we will learn how to convert array to comma separated string in PHP
basically, in PHP we use implode function to convert the array into comma separated string. And with implode function convert one dimensional, two dimensional and multidimensional array to comma separated string. In this tutorial, we will learn how to convert array to comma separated string using implode function
PHP Convert Array to Comma Separated String
PHP implode function is used to convert an array into a string.
This tutorial shows how to convert string to an array, two-dimensional array, and multi-dimensional in PHP.
Syntax of using the PHP implode method
The basic syntax is of the implode function is:
implode(separator,array)
Parameters of implode function:
Parameter | Description |
---|---|
separator | Optional. Specifies what to put between the array elements. Default is “” (an empty string) |
array | Required. The array to join to a string |
Examples – Convert an Array to String in PHP
Let’s see the following examples to convert one dimensional, two dimensional and multidimensional array to comma separated string in PHP:
- PHP Array to String Conversion
- PHP Array to Comma Separated String
- PHP Two Dimensional Array Convert to Comma Separated String
- PHP Implode – Multidimensional Array to Comma Separated String
PHP Array to String Conversion
Let’s take an example for index array convert to string in PHP:
<?php
$array = array("PHP","PYTHON","JAVA");
$string =implode(" ", $array);
echo "The returned string after implod: <br><strong>".$string ."</strong>";
?>
Index array convert to String example source code
<!DOCTYPE html>
<html>
<title>PHP Array to String Conversion</title>
<head></head>
<body>
<?php
$array = array("PHP","PYTHON","JAVA");
$string =implode(" ", $array);
echo "The returned string after implode with comma: <br><strong>".$string ."</strong>";
?>
</body>
</html>
PHP Array to Comma Separated String
Let’s take new example with an array, here we will convert array to a comma-separated string.
<?php
$array = array("PHP","PYTHON","JAVA");
$string =implode(",", $array);
echo "The returned string after implode with comma: <br><strong>".$string ."</strong>";
?>
Array to Comma Separated String Example Source Code
<!DOCTYPE html>
<html>
<title>PHP Array to Comma Separated String</title>
<head></head>
<body>
<?php
$array = array("PHP","PYTHON","JAVA");
$string =implode(",", $array);
echo "The returned string after implode with comma: <br><strong>".$string ."</strong>";
?>
</body>
</html>
PHP Two Dimensional Array Convert to Comma Separated String
Now we will take the example of a two-dimensional array. In this example, we will convert two-dimensional array to comma separate string.
<?php
// Two Dimensional array
$array = array (
array ('01','03','02','15'),
array ('05','04','06','10'),
array ('07','09','08','11'),
array ('12','14','13','16')
);
$tmpArr = array();
foreach ($array as $sub) {
$tmpArr[] = implode(',', $sub);
}
$result = implode(',', $tmpArr);
echo $result;
Two Dimensional Array to Comma Separated String Example Source Code
<!DOCTYPE html>
<html>
<title>PHP Two Dimensional Array to Comma Separated String</title>
<head></head>
<body>
<?php
// Two Dimensional array
$array = array (
array ('01','03','02','15'),
array ('05','04','06','10'),
array ('07','09','08','11'),
array ('12','14','13','16')
);
$tmpArr = array();
foreach ($array as $sub) {
$tmpArr[] = implode(',', $sub);
}
$result = implode(',', $tmpArr);
echo $result;
?>
</body>
</html>
PHP Implode – Multidimensional Array to Comma Separated String
Here you will learn how you can convert multidimensional array to comma separated string.
<?php
$multi_dimensional_array = array(
array(
'Name' => 'PHP',
'Email' => '[email protected]'
),
array(
'Name' => 'Java',
'Email' => '[email protected]'
)
);
echo implode(', ', array_map(function ($string) {
return $string['Name'];
}, $multi_dimensional_array));
?>
Multi-Dimensional Array to Comma Separated String Example Source Code
<!DOCTYPE html>
<html>
<title>PHP Multi Dimensional Array Convert to Comma Separated String</title>
<head></head>
<body>
<?php
$multi_dimensional_array = array(
array(
'Name' => 'PHP',
'Email' => '[email protected]'
),
array(
'Name' => 'Java',
'Email' => '[email protected]'
)
);
echo implode(', ', array_map(function ($string) {
return $string['Name'];
}, $multi_dimensional_array));
?>
</body>
</html>
Conclusion
In this tutorial, we have learned how to convert an array to comma separated string using implode function
Recommend PHP Tutorials
- PHP Array: Indexed,Associative, Multidimensional
- To Remove Elements or Values from Array PHP
- PHP remove duplicates from multidimensional array
- Remove Duplicate Elements or Values from Array PHP
- How to Convert String to Array in PHP
- Array Push and POP in PHP | PHP Tutorial
- PHP Search Multidimensional Array [key and value and return key]