PHP array to comma-separated string

PHP array to comma-separated string

Here you will learn, php array to comma separated string, 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.

This tutorial demonstrates, how to convert array to string in PHP using the PHP implode function.

Array to String Conversion in PHP

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

This tutorial shows you 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:

ParameterDescription
separatorOptional. Specifies what to put between the array elements. Default is “” (an empty string)
arrayRequired. The array to join to a string

Examples – Convert an Array to 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

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.

You may like

  1. PHP Array: Indexed,Associative, Multidimensional
  2. To Remove Elements or Values from Array PHP
  3. PHP remove duplicates from multidimensional array
  4. Remove Duplicate Elements or Values from Array PHP
  5. How to Convert String to Array in PHP
  6. Array Push and POP in PHP | PHP Tutorial
  7. PHP Search Multidimensional Array [key and value and return key]

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *