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

How to convert array to string in php?

青灯夜游
青灯夜游Original
2020-04-24 17:09:022457browse

How to convert array to string in php? The following article will introduce it to you. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to convert array to string in php?

In PHP, you can use the explode() function to convert a string into a one-dimensional array, or you can use a function with the opposite function - implode( ) to convert the array into a string.

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

implode($glue, $array) 或者 implode($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.

Tip: The $glue parameter of the implode() function is optional and can be omitted.

[Example] Use the implode() function to convert an array into a string.

<?php
    header("Content-Type: text/html; charset=utf-8");
    $arr = [&#39;php中文网&#39;,&#39;PHP教程&#39;,&#39;https://www.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;;
?>

The running results are as follows:

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

For more related knowledge, please pay attention to PHP Chinese website! !

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