Home  >  Article  >  Backend Development  >  How to convert php array value to string

How to convert php array value to string

藏色散人
藏色散人Original
2021-03-22 09:05:442448browse

In PHP, you can use the implode function to convert the PHP array value into a string. The syntax is "implode(separator,array)", where the parameter separator specifies the content placed between the array elements.

How to convert php array value to string

The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer

implode() function returns a combination of array elements String.

Note: The implode() function accepts two parameter orders. However, due to historical reasons, explode() does not work. You must ensure that the separator parameter comes before the string parameter.

Note: The separator parameter of the implode() function is optional. However, for backward compatibility, it is recommended that you use two parameters.

Note: This function is binary safe.

Syntax

implode(separator,array)

Parameters

separator Optional. Specifies what is placed between array elements. Default is "" (empty string).

array Required. Arrays to be combined into strings.

Example 1

Separate array elements with different characters:

<?php
$arr = array(&#39;Hello&#39;,&#39;World!&#39;,&#39;I&#39;,&#39;love&#39;,&#39;Shanghai!&#39;);
echo implode(" ",$arr)."<br>";
echo implode("+",$arr)."<br>";
echo implode("-",$arr)."<br>";
echo implode("X",$arr);
?>

Running results:

Hello World! I love Shanghai!
Hello+World!+I+love+Shanghai!
Hello-World!-I-love-Shanghai!
HelloXWorld!XIXloveXShanghai!

Example 2

Separate array elements Combined into a string:

<?php
$arr = array(&#39;Hello&#39;,&#39;World!&#39;,&#39;I&#39;,&#39;love&#39;,&#39;Shanghai!&#39;);
echo implode(" ",$arr);
?>

Running result:

Hello World! I love Shanghai!

[Recommended learning: PHP video tutorial]

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