


PHP array learning: splicing elements into a string and outputting it (3 methods)
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; use in each loop .=
or .
operator to splice arrays together.
Let’s take a look at the implementation code:
<?php header("Content-type:text/html;charset=utf-8"); $array= array("香蕉","苹果","梨子","橙子","橘子","榴莲"); $str=''; foreach ($array as $value) { $str.=$value; } var_dump($str); ?>
The output result is:
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:
<?php header("Content-type:text/html;charset=utf-8"); $array= array("香蕉","苹果","梨子","橙子","橘子","榴莲"); $str=implode($array); var_dump($str); $str=implode('-',$array); var_dump($str); $str=implode('::',$array); var_dump($str); ?>
The output result is:
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:
<?php header("Content-type:text/html;charset=utf-8"); $array= array("香蕉","苹果","梨子","橙子","橘子","榴莲"); function f($v1,$v2) { return $v1 . $v2; } $str=array_reduce($array,"f"); print_r($str); ?>
The output result is:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
