PHP array to comma separated string

Convert array to comma seperated string in php; In this tutorial, i am going to show you how to convert array to comma separated string in php, PHP Array to String Conversion, PHP Array to Comma Separated String, PHP Two Dimensional Array to String Conversion, PHP Implode – Multidimensional Array to Comma Separated String, PHP Implode Multidimensional Array to Comma Separated String.

PHP implode function is used to convert an array into a string.

PHP Convert Array to Comma Separated String

Convert an Array to String in PHP

Now, i will take following example of how to convert string to an array, two-dimensional array, and multi-dimensional in PHP; as follows:

  • 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>";
?>

PHP Array to Comma Separated String

Let’s take new example of 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>";
?>

PHP Two Dimensional Array Convert to Comma Separated String

To convert two-dimensional array to comma separate string; as follows:

<?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;

PHP Implode – Multidimensional Array to Comma Separated String

Let’s take example of how to 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));
?>

Conclusion

Array to string conversion in the PHP tutorial. You have learned how to convert index, two dimensional, and multidimensional array to string or comma-separated string with example.

Recommend PHP Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.