Home  >  Article  >  Backend Development  >  How to convert PHP array to string?

How to convert PHP array to string?

Guanhui
GuanhuiOriginal
2020-06-20 16:22:482924browse

How to convert PHP array to string?

#How to convert PHP array to string?

In PHP, you can use the "implode()" function to convert an array into a string. This function is used to convert the values ​​of a one-dimensional array into a string. Its syntax is "implode( glue,pieces)", its parameter glue represents the connector between array elements, and the parameter pieces represents the array to be converted.

Simple example

<?php

$array = array(&#39;lastname&#39;, &#39;email&#39;, &#39;phone&#39;);
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

// Empty string when using an empty array:
var_dump(implode(&#39;hello&#39;, array())); // string(0) ""

?>

Usage example

<?php
    $a1 = array("1","2","3");
    $a2 = array("a");
    $a3 = array();
   
    echo "a1 is: &#39;".implode("&#39;,&#39;",$a1)."&#39;<br>";
    echo "a2 is: &#39;".implode("&#39;,&#39;",$a2)."&#39;<br>";
    echo "a3 is: &#39;".implode("&#39;,&#39;",$a3)."&#39;<br>";
?>

Output result:

a1 is: &#39;1&#39;,&#39;2&#39;,&#39;3&#39;
a2 is: &#39;a&#39;
a3 is: &#39;&#39;

Recommended tutorial: "PHP"

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