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

How to convert array to string in php?

青灯夜游
青灯夜游Original
2020-11-04 14:00:463508browse

In PHP, you can use the implode() function to convert an array into a string. It can convert a one-dimensional array into a string; the syntax format is "implode(array)", which will return a A string composed of array elements.

How to convert array to string in php?

Recommended: "PHP Video Tutorial"

implode() function can convert a one-dimensional array into characters String, returns a string composed of array elements.

The syntax format is as follows:

implode($glue, $array) 
//或者 
implode($array)

Parameters:

  • $glue is used to set a string, specified The content placed between the array elements, that is, using $glue to connect each element of the array together. By default, $glue is an empty string ("");

  • $array is Array to be converted.

Tip: The $glue parameter of the implode() function is optional and can be omitted. However, for backward compatibility, it is recommended that you use two parameters.

Return value: A string composed of array elements.​

Example:Use the implode() function to convert the array into a string.

<?php
    header(&#39;content-type:text/html;charset=utf-8&#39;);
    $arr = [&#39;php中文网&#39;,&#39;PHP教程&#39;,&#39;http://php.cn&#39;,&#39;implode()函数&#39;,&#39;数组转字符串&#39;];
    $str = implode($arr);
    echo $str.&#39;<br>&#39;;
    $str = implode(&#39; &#39;,$arr);
    echo $str.&#39;<br>&#39;;
    $str = implode(&#39;,&#39;,$arr);
    echo $str.&#39;<br>&#39;;
    $str = implode(&#39;--&#39;,$arr);
    echo $str.&#39;<br>&#39;;
?>

Output:

php中文网PHP教程http://php.cnimplode()函数数组转字符串
php中文网 PHP教程 http://php.cn implode()函数 数组转字符串
php中文网,PHP教程,http://php.cn,implode()函数,数组转字符串
php中文网--PHP教程--http://php.cn--implode()函数--数组转字符串

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of How to convert array to string 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