$value){$str .=','.$value; }"; 2. Use implode() Function, syntax "implode(',',$arr)"."/> $value){$str .=','.$value; }"; 2. Use implode() Function, syntax "implode(',',$arr)".">
Home > Article > Backend Development > How to convert array to string in php
Conversion method: 1. Use the foreach statement and the ".=" symbol, the syntax "foreach($arr as $key => $value){$str .=','.$value; }" ;2. Use the implode() function, the syntax is "implode(',',$arr)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php Lieutenant General Array Convert to string
#Method 1: Use the foreach statement and the ".=" character splicing character
Use first The foreach statement traverses the array
Use ".=" to splice the array elements together to form a string.
<?php $arr = ['Lucy','Mike','Jery','Haly']; $str = ''; foreach ($arr as $key => $value) { $str .=','.$value; } var_dump($str); ?>
2. Use the implode function
<?php $arr = ['Lucy','Mike','Jery','Haly']; $str = implode(',', $arr); var_dump($str); ?>
implode () function returns a string composed of array elements.
Syntax
implode(separator,array)
separator is optional. Specifies what is placed between array elements. Default is "" (empty string).
array required. Arrays to be combined into strings
Recommended learning: "PHP ARRAY"
The above is the detailed content of How to convert array to string in php. For more information, please follow other related articles on the PHP Chinese website!