Home >Backend Development >PHP Problem >PHP array learning: splicing elements into a string and outputting it (3 methods)

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

青灯夜游
青灯夜游Original
2021-08-31 17:55:186884browse

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=&#39;&#39;;
foreach ($array as $value) { 
    $str.=$value;
} 
var_dump($str);
?>

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:

<?php
header("Content-type:text/html;charset=utf-8");
$array= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
$str=implode($array);
var_dump($str);

$str=implode(&#39;-&#39;,$array);
var_dump($str);

$str=implode(&#39;::&#39;,$array);
var_dump($str);
?>

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:

<?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:

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!

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