The two most common variable types in PHP are array and string, in the process of development, it is inevitable to encounter how to convert string to array, or array to 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 to convert between strings and arrays. That is, the explode() function can complete the string into an array, and the implode() function can complete the array into a string.

So let's learn the use of these two functions and how to achieve the mutual conversion between strings and arrays.

explode() function - string to array

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

1

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

One thing to note is this.

The key point in trying to understand this function is the delimiter, which is the parameter $delimiter, which indicates the separator character used to separate strings, and it is used to mark what is used to turn into separate array elements. The $string parameter is the string used to separate the strings, and the $limit parameter is an optional parameter that can be empty.

Next we look at the usage of the explode() function through an example, which is as follows.

1

2

3

4

5

6

7

<?php

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";

$pieces = explode(" ", $pizza);

echo $pieces[0] . '<br/>'; // piece1

echo $pieces[1] . '<br/>'; // piece2

print_r($pieces);

?>

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

It should be noted that when the argument $limit is empty, the function returns all the elements of the array; when the argument $limit is positive, the maximum number of elements that can be included in the array is the number of $limit; when the argument $limit is negative, it returns all the elements except the last $limit; if the argument $limit is 0, it is treated as 1.

Examples are as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<?php

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";

$pieces = explode(" ", $pizza);

echo $pieces[0] . '<br/>'; // piece1

echo $pieces[1] . '<br/>'; // piece2

print_r($pieces);

echo '<br/>';

$pieces = explode(" ", $pizza , 2);

print_r($pieces);

echo '<br/>';

$pieces = explode(" ", $pizza , 0);

print_r($pieces);

echo '<br/>';

$pieces = explode(" ", $pizza , -2);

print_r($pieces);

?>

The above example returns different results with different $limit arguments, but the explode() function completes the conversion from a string to an array. Next, let's look at how arrays are converted to strings.

implode()-Array to string conversion

A one-dimensional array can be converted to a string in PHP by the implode() function, which has the following syntax format.

1

implode($glue, $array)

One thing to note is that $glue can be seen as a glue character, its role is to join each element of the array together, in the default case $glue is an empty string. The $array parameter is the string to be transformed.

Next, let's look at the use of the implode() function with the following example.

1

2

3

4

5

<?php

    $arr = ['1','2','3','4','5'];

    $str = implode($arr);

    echo $str.'<br>';

?>

In the above example, the parameter $glue is set to null, which means that it can be left out, but if it is, the glue character will be inserted in the middle of each array element to convert it to a string.

Examples are as follows:

1

2

3

4

5

6

7

8

9

10

11

<?php

    $arr = ['1','2','3','4','5'];

    $str = implode($arr);

    echo $str.'<br>';

    $str = implode(',' , $arr);

    echo $str.'<br>';

    $str = implode('---' , $arr);

    echo $str.'<br>';

    $str = implode('/' , $arr);

    echo $str

?>

The above example is an array to string conversion with a different parameter $glue.

Conversion of strings to arrays

Let's look at the interconversion between arrays and strings with the following example.

1

2

3

4

5

6

7

8

9

10

<?php

    $arr = ['1','2','3','4','5'];

    $str = implode($arr);

    echo $str.'<br>';

    $arr1 = explode(" ", $str);

    print_r($arr1);

    echo '<br/>';

    $str1 = implode($arr1);

    echo $str1.'<br>';

?>

From the above example, we understand that we can convert between arrays and strings by using the implode() and explode() functions.

keywords: php