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

How to convert php array to string

L
LOriginal
2020-05-29 17:41:124265browse

How to convert php array to string

php array to string

PHP array to string Two methods for strings

Method one, use the built-in implode function

Method two, use a loop to traverse the array elements to splice them into a string

<?php
// PHP数组转字符串的方法
// 方法一:implode(glue, pieces)
$arr = [&#39;Lucy&#39;,&#39;Mike&#39;,&#39;Jery&#39;,&#39;Haly&#39;];
$str = implode(&#39;,&#39;, $arr);
// var_dump($str);
//方法二,利用循环遍历数组元素拼接成字符串
$arr1 = [&#39;Lucy&#39;,&#39;Mike&#39;,&#39;Jery&#39;,&#39;Haly&#39;];
$str1 = &#39;&#39;;
foreach ($arr1 as $key => $value) 
{
$str1 .=&#39;,&#39;.$value; 
}
var_dump($str1);

Recommended tutorial: " PHP Tutorial》

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