Home>Article>Backend Development> Teach you how to convert arrays and strings into each other in PHP in five minutes
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!
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, theexplode()
function can convert a string into an array, and theimplode()
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.
explode()
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:
'; // piece1 echo $pieces[1] . '
'; // piece2 print_r($pieces); ?>
Output result:
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
isempty
, 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:
'; // piece1 echo $pieces[1] . '
'; // piece2 print_r($pieces); echo '
'; $pieces = explode(" ", $pizza , 2); print_r($pieces); echo '
'; $pieces = explode(" ", $pizza , 0); print_r($pieces); echo '
'; $pieces = explode(" ", $pizza , -2); print_r($pieces); ?>
Output results:
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.
implode()
-Convert array to string
In PHP, a one-dimensional array can be converted into a string through theimplode()
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:
'; ?>
Output result:
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:
'; $str = implode(',' , $arr); echo $str.'
'; $str = implode('---' , $arr); echo $str.'
'; $str = implode('/' , $arr); echo $str ?>
Output result:
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 :
'; $arr1 = explode(" ", $str); print_r($arr1); echo '
'; $str1 = implode($arr1); echo $str1.'
'; ?>
Output result:
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!