$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

How to convert array to string in php

青灯夜游
青灯夜游Original
2022-02-14 18:37:326636browse

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

How to convert array to string in php

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 = [&#39;Lucy&#39;,&#39;Mike&#39;,&#39;Jery&#39;,&#39;Haly&#39;];
$str = &#39;&#39;;
foreach ($arr as $key => $value) 
{
	$str .=&#39;,&#39;.$value; 
}
var_dump($str);

?>

How to convert array to string in php

2. Use the implode function

<?php
$arr = [&#39;Lucy&#39;,&#39;Mike&#39;,&#39;Jery&#39;,&#39;Haly&#39;];
$str = implode(&#39;,&#39;, $arr);
var_dump($str);
?>

How to convert array to string in php

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!

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