Detailed explanation of two methods of converting PHP arrays to strings [Video attached]

藏色散人
Release: 2019-11-25 09:53:28
Original
12694 people have browsed it

This article mainly introduces two methods for converting PHP arrays to strings. (The corresponding video tutorial is attached at the end of the article)

The first method: Use PHP's own function implode to directly convert the array into a string.

Second method: Use a loop to traverse the array elements and concatenate them into a string.

Recommended manual:php complete self-study manual

Below we will explain it to you one by one through specific code examples.

Code sample method 1:

<?php
$arr=[&#39;PHP&#39;,&#39;Python&#39;,&#39;Java&#39;,&#39;C&#39;];
$str=implode(glue:&#39; &#39;,$arr);
 var_dump($arr);
 var_dump($str);
Copy after login

In the above code, first we create an $arr array, which is assigned four different values; then we use the implode function to $arr Arrays are converted to strings. Everyone should understand that there are two parameters in the PHP implode() function. The first parameter glue:’ ’ represents a splicing character that connects the elements in the array, and the second parameter is the array we need to convert; here we use the $str variable to receive the value of the array converted to a string. Finally, use var_dump to print out the two variable values ​​separately.

Then we access it through the browser, and the printed result is as shown below:

Detailed explanation of two methods of converting PHP arrays to strings [Video attached]

You can find that the $arr array has been converted into a string.

Code sample method 2:

<?php
$arr=[&#39;PHP&#39;,&#39;Python&#39;,&#39;Java&#39;,&#39;C&#39;];
$str=&#39; &#39;;
foreach ($arr as $v){
   $str1 .=&#39; &#39;.$v;
}
var_dump($str1);
Copy after login

In this code, we first define a $str1 variable and assign it to empty, and then use a foreach loop to change each value in the $arr array Assign values ​​to $v respectively, and then use dots to connect the strings. Finally, use var_dump to print out $str1 to see if the return value of $str1 is a string.

We continue to access through the browser, and the displayed result is as follows:

Detailed explanation of two methods of converting PHP arrays to strings [Video attached]

You should also be able to see that the array can be converted into characters through loop traversal. String display.

This article also has a detailed video tutorial [Two methods of converting php arrays to strings] for your reference.

Recommended related articles:
1.Explanation of PHP string to array and array to string functions
Related video recommendations
1.Dugu Jiujian (4)_PHP video tutorial

The above content is a detailed introduction to the two methods of converting PHP arrays into strings, which has certain Reference value, I hope it will be of some help to friends in need.

The above is the detailed content of Detailed explanation of two methods of converting PHP arrays to strings [Video attached]. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template