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

How to convert array to string in php?

青灯夜游
Release: 2023-02-28 18:56:01
Original
2550 people have browsed it

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

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

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()函数--数组转字符串
Copy after login

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!

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