Home > Article > Backend Development > 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('lastname', 'email', 'phone'); $comma_separated = implode(",", $array); echo $comma_separated; // lastname,email,phone // Empty string when using an empty array: var_dump(implode('hello', array())); // string(0) "" ?>
Usage example
<?php $a1 = array("1","2","3"); $a2 = array("a"); $a3 = array(); echo "a1 is: '".implode("','",$a1)."'<br>"; echo "a2 is: '".implode("','",$a2)."'<br>"; echo "a3 is: '".implode("','",$a3)."'<br>"; ?>
Output result:
a1 is: '1','2','3' a2 is: 'a' a3 is: ''
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!