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

How to convert array to string in php separated by commas

青灯夜游
青灯夜游Original
2021-05-28 11:54:113732browse

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)".

How to convert array to string in php separated by commas

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:

How to convert array to string in php separated by commas

##Description:## The #join() function returns a string composed of array elements. The syntax is as follows:

join(separator,array)

##ParameterDescriptionseparatorarrayReturn value: Returns a string composed of array elements.
Optional. Specifies what is placed between array elements, the delimiter. Default is "" (empty string).
Required. Arrays to be combined into strings.

Method 2: Use the implode() function

<?php
$arr =array("red","green","blue","yellow","brown");
$str = implode(",",$arr);
print_r($str);
?>
Output:
red,green,blue,yellow,brown

Description:

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!

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