Home  >  Article  >  Backend Development  >  Teach you how to convert arrays and strings into each other in PHP in five minutes

Teach you how to convert arrays and strings into each other in PHP in five minutes

WBOY
WBOYOriginal
2021-11-03 18:16:562479browse

In the previous article "How to handle the type of PHP array" we introduced the array types in PHP in detail. In this article we will take a look at how to implement arrays and strings in PHP. I hope it will be helpful to everyone with relevant knowledge about mutual conversion!

Teach you how to convert arrays and strings into each other in PHP in five minutes

The two most commonly used variable types in PHP are arrays and strings. During the development process, you will inevitably encounter the problem of how to convert strings into arrays. situation, or the situation of converting an array into a string.

When encountering such a situation, how should we solve it? These two variable types are so common and commonly used. There are two functions in PHP that can perform interaction between strings and arrays. Convert. That is, the explode() function can convert a string into an array, and the implode() function can convert an array into a string.

Then let’s learn the use of these two functions together, and how to convert strings and arrays to each other.

<strong><span style="font-size: 20px;">explode()</span></strong>Function - Convert string to array

explode()The function can use a string to split another string and then return the result is an array. The syntax format of the explode() function is as follows:

explode($delimiter, $string [, $limit])

What needs to be noted is:

The key point to understand this function is the delimiter, which is the parameter $delimiter, which represents the delimiter character used to separate strings. It is used to mark what is used to become an independent array element. The parameter $string represents the string used to separate, and the parameter $limit is an optional parameter and can be empty.

Next let’s take a look at the usage of the explode() function through an example. The example is as follows:

<?php
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0] . &#39;<br/>&#39;; // piece1
echo $pieces[1] . &#39;<br/>&#39;; // piece2
print_r($pieces);
?>

Output result:

Teach you how to convert arrays and strings into each other in PHP in five minutes

Through the above example, we can find that the explode() function converts a string into an array, and the function parameter $limit is empty in the above example.

It should be noted that when the parameter $limit is empty, the function returns all array elements; when the parameter $limit is When the number is positive, the maximum number of elements in the returned array is the number filled in $limit; when the parameter $limit is a negative number, all elements except the last $limit elements are returned. Element; if parameter $limit is 0, it is treated as 1.

The examples are as follows:

<?php
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0] . &#39;<br/>&#39;; // piece1
echo $pieces[1] . &#39;<br/>&#39;; // piece2
print_r($pieces);
echo &#39;<br/>&#39;;
$pieces = explode(" ", $pizza , 2);
print_r($pieces);
echo &#39;<br/>&#39;;
$pieces = explode(" ", $pizza , 0);
print_r($pieces);
echo &#39;<br/>&#39;;
$pieces = explode(" ", $pizza , -2);
print_r($pieces);
?>

Output results:

Teach you how to convert arrays and strings into each other in PHP in five minutes

The above examples are the results returned by different $limit parameters Different, but the conversion of string to array is completed through the explode() function. Next we look at how to convert an array into a string.

<strong><span style="font-size: 20px;">implode()</span></strong>-Convert array to string

In PHP, a one-dimensional array can be converted into a string through the implode() function. The syntax format of this function is as follows:

implode($glue, $array)

What needs to be noted is: $glue can be regarded as a glue symbol. Its function is to connect each element in the array together. By default, the parameter $glue is an empty string. Parameter $array represents the string that needs to be converted.

Next, let’s take a look at the use of the implode() function through an example. The example is as follows:

<?php
    $arr = [&#39;元素1&#39;,&#39;元素2&#39;,&#39;元素3&#39;,&#39;元素4&#39;,&#39;元素5&#39;];
    $str = implode($arr);
    echo $str.&#39;<br>&#39;;
?>

Output result:

Teach you how to convert arrays and strings into each other in PHP in five minutes

In the above example, the parameter $glue is set to empty, which means that this parameter does not need to be written, but if it is written, the glue character will be inserted into the middle of each array element to convert it into a string.

The example is as follows:

<?php
    $arr = [&#39;元素1&#39;,&#39;元素2&#39;,&#39;元素3&#39;,&#39;元素4&#39;,&#39;元素5&#39;];
    $str = implode($arr);
    echo $str.&#39;<br>&#39;;
    $str = implode(&#39;,&#39; , $arr);
    echo $str.&#39;<br>&#39;;
    $str = implode(&#39;---&#39; , $arr);
    echo $str.&#39;<br>&#39;;
    $str = implode(&#39;/&#39; , $arr);
    echo $str
?>

Output result:

Teach you how to convert arrays and strings into each other in PHP in five minutes

In the above example, it is done through different parameters $glue Array to string conversion.

Mutual conversion between strings and arrays

Let’s take a look at the mutual conversion between arrays and strings through examples. The examples are as follows :

<?php
    $arr = [&#39;元素1&#39;,&#39;元素2&#39;,&#39;元素3&#39;,&#39;元素4&#39;,&#39;元素5&#39;];
    $str = implode($arr);
    echo $str.&#39;<br>&#39;;
    $arr1 = explode(" ", $str);
    print_r($arr1);
    echo &#39;<br/>&#39;;
    $str1 = implode($arr1);
    echo $str1.&#39;<br>&#39;;
?>

Output result:

Teach you how to convert arrays and strings into each other in PHP in five minutes

From the above example, we understand that arrays and arrays can be completed through the implode() function and explode() function. Convert between strings.

If you are interested, you can click on "PHP Video Tutorial" to learn more about PHP knowledge.

The above is the detailed content of Teach you how to convert arrays and strings into each other in PHP in five minutes. 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