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

How to convert array into string in php

藏色散人
藏色散人Original
2020-12-01 09:30:042051browse

php method to convert an array into a string: first create a PHP sample file; then define an array data as "$arr"; then use the "implode($arr);" method to convert the array into String; finally output the conversion result.

How to convert array into string in php

Recommended: "PHP Video Tutorial"

The operating environment of this tutorial: Windows 7 system, PHP version 5.6, This method works for all brands of computers.

PHP implode(): Array to string

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

implode($glue, $array) or implode($array)

Among them, $glue is used to set a string, indicating that $glue is used to connect 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
    $arr = [&#39;PHP中文网&#39;,&#39;PHP教程&#39;,&#39;//m.sbmmt.com/php/&#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教程//m.sbmmt.com/php/implode()函数数组转字符串
PHP中文网 PHP教程 //m.sbmmt.com/php/ implode()函数 数组转字符串
PHP中文网,PHP教程,//m.sbmmt.com/php/,implode()函数,数组转字符串
PHP中文网--PHP教程--//m.sbmmt.com/php/--implode()函数--数组转字符串

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