PHP array learning: splicing elements into a string and outputting it (3 methods)

青灯夜游
Release: 2023-03-12 08:50:02
Original
6759 people have browsed it

In the previous article "PHP String Learning: Dividing Strings into Substrings of Smaller Length", we introduced a way to split strings and pass multiple substrings into an array as Method for converting array elements (i.e. string to array). This time we will talk about converting arrays to strings and introduce how to use PHP to splice array elements into a string. Interested friends can learn about it~

→Related recommendations:PHP array learning series summary (continuously updated~)

The main content of today’s article is: convert the array into a string, splice all the elements in the array together to form an string and output.

So how to achieve it? The following article will share with you three operation methods. First, we will introduce the most familiar loop array splicing, and then we will take you to understand the two built-in functions and see how they operate.

Method 1: Use the foreach statement to traverse the array

Implementation idea: use the foreach statement to traverse the array; usein each loop .=or.operator to splice arrays together.

Let’s take a look at the implementation code:

Copy after login

The output result is:

PHP array learning: splicing elements into a string and outputting it (3 methods)

Method 2: Use implode () function

implode([$glue, ]$array)function can connect each array element according to the delimiter$glue, if the $glue parameter is omitted, it will be separated by an empty string by default.

Let’s take a look at the implementation code:

Copy after login

The output result is:

PHP array learning: splicing elements into a string and outputting it (3 methods)

Method 3: Use array_reduce ()Function

array_reduce() function sends the value in the array to the user-defined function (callback function) and returns a string.

Syntax:array_reduce(array, myfunction,initial)

  • array: required parameters, array object that needs to be processed

  • myfunction: required parameters, the name of the callback function, the syntax is:function myfunction(previousValue, currentVaule), up to two parameters can be accepted:

    • previousValue: carries the return value of the previous iteration; if this iteration is the first, then this value is initial.

    • currentVaule: carries the value of this iteration.

  • initial: Optional parameter, if the optional parameter initial is specified, this parameter will be used as the initial value at the beginning of processing. If the array is empty, then Will be returned as the final result

Let’s take a look at the implementation code:

Copy after login

The output result is:

PHP array learning: splicing elements into a string and outputting it (3 methods)

Okay, that’s it for now. If you want to know anything else, you can click here. → →php video tutorial

Finally, I would like to recommend a free video tutorial on PHP arrays:PHP function array array function video explanation, come and learn!

The above is the detailed content of PHP array learning: splicing elements into a string and outputting it (3 methods). 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!