Conversion method: 1. Use the join() function to return a string composed of array elements, with the syntax format "join(",",array)"; 2. Use the implode() function, A one-dimensional array can be converted into a string with the syntax format "mplode(",",array)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Method 1: Use join() function
<?php $data = array("red","green","blue","yellow","brown"); $str = join(",", $data); var_dump($str); ?>
Output:
##Description:## The #join() function returns a string composed of array elements. The syntax is as follows:
join(separator,array)
Optional. Specifies what is placed between array elements, the delimiter. Default is "" (empty string). | |
Required. Arrays to be combined into strings. |
<?php
$arr =array("red","green","blue","yellow","brown");
$str = implode(",",$arr);
print_r($str);
?>
red,green,blue,yellow,brown
implode() function can convert a one-dimensional array into a string. Its syntax format is as follows:
implode(separator,array)
Recommended learning: "
PHP Video Tutorial
The above is the detailed content of How to convert array to string in php separated by commas. For more information, please follow other related articles on the PHP Chinese website!