How to convert array to string in php separated by commas

青灯夜游
Release: 2023-03-10 07:06:02
Original
3741 people have browsed it

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);
?>
Copy after login

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)
Copy after login

##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);
?>
Copy after login
Output:
red,green,blue,yellow,brown
Copy after login

Description:

implode() function can convert a one-dimensional array into a string. Its syntax format is as follows:

implode(separator,array)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!