Home > Article > Backend Development > How to convert array to string with commas in php
In php, you can use the implode() function to convert the array into a string with commas, the syntax is "implode(",",$arr)"; this function can convert the array into a string, when the When the first parameter of the function is set to ",", you can use commas as the connector to connect each element of the array together to form a string.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
#php, you can use implode( ) function converts an array to a string with commas.
implode() function can convert a one-dimensional array into a string. Its syntax format is as follows:
implode([$glue,]$array)
Among them, $glue is used to set a string, indicating the use of $ Glue connects each element of the array together. By default, $glue is an empty string; $array is the array that needs to be converted.
You only need to set the $glue parameter to "," to use commas as the connector to connect each element of the array together to form a string.
Example:
<?php $arr = array(1,2,3,4,5,6,7,8,9); $str = implode(",",$arr); echo $str; var_dump($str); ?>
Explanation: The $glue parameter of the implode() function is optional and can be omitted; then the default connector is ""
(empty string)
<?php $arr = array(1,2,3,4,5,6,7,8,9); $str = implode(" ",$arr); echo $str; var_dump($str); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert array to string with commas in php. For more information, please follow other related articles on the PHP Chinese website!