Home  >  Article  >  Backend Development  >  How to merge arrays into strings in php

How to merge arrays into strings in php

青灯夜游
青灯夜游Original
2022-05-26 20:52:462987browse

Implementation method: 1. Use the array_merge() function to merge multiple arrays into one array, the syntax is "array_merge(array 1, array 2, array 3...)"; 2. Use the implode() function Convert the merged array to a string using the syntax "implode("connection character", merged array)".

How to merge arrays into strings in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Required to convert arrays into strings Divided into two parts:

  • Merge arrays: merge multiple arrays into one array

  • Array to string: merge Convert the resulting array to a string

Implementation steps:

1. Use the array_merge() function to merge arrays

array_merge(array1,array2,array3...)You can merge two or more arrays. When the keys of the two arrays are the same, the value of the latter array overwrites the value of the previous array.

<?php
$a = [1,&#39;2&#39;=>2,&#39;a&#39;=>&#39;a&#39;,&#39;b&#39;=>&#39;b&#39;];
$b = [1,&#39;2&#39;=>3,&#39;a&#39;=>&#39;c&#39;,&#39;b&#39;=>&#39;d&#39;,&#39;c&#39;=>&#39;e&#39;];
$c = array_merge($a,$b);

var_dump($a);
var_dump($b);
var_dump($c);
?>

How to merge arrays into strings in php

2. Use the implode() function to convert the merged array to a string

implode($glue ,$arr)The function can convert a one-dimensional array into a string, and will return a string composed of array elements and the "$glue" character.

var_dump($c);
var_dump(implode($c));
var_dump(implode("",$c));
var_dump(implode(",",$c));
var_dump(implode("-",$c));
var_dump(implode("::",$c));

How to merge arrays into strings in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to merge arrays into strings 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