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

How to convert PHP array to string?

Guanhui
Release: 2023-03-01 16:24:01
Original
3033 people have browsed it

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) ""

?>
Copy after login

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

Output result:

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

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!

Related labels:
php
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