Home  >  Article  >  Backend Development  >  How to convert array to string with commas in php

How to convert array to string with commas in php

青灯夜游
青灯夜游Original
2022-04-14 20:54:592375browse

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.

How to convert array to string with commas in php

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

How to convert array to string with commas in php

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

How to convert array to string with commas in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn