In PHP, arrays and strings are so common that PHP has two functions that can be converted between strings and arrays.
$array=explode(separator,$string); $string=implode(glue,$array);
The key to using and understanding these two functions is the relationship between the separator and glue. When an array is converted into a string, the delimiter is set-the character or code that is inserted into the array value in the generated string.
On the contrary, when you convert a string into an array, you must specify a delimiter to mark what should be converted into an independent array element. For example, start with a string:
$ S1 = 'mon-Tue-Wed-Thu-fri ';
$ Days_array = explode ('-', $ s1 );
The $ days_array variable is now an array with five elements. the Mon index of the element is 0, and the Tue index is 1.
$ S2 = implode (',', $ days_array );
$ S2
Variables are now a list of days in a week separated by commas: Mon, Tue, Wed, Thu, Fri
Example 1. explode () example
Example 2. limit parameter example
The above example will be output:
Array
(
[0] => one
[1] => two | three | four
)
Array
(
[0] => one
[1] => two
[2] => three
)
Note: This function can be safely used for binary objects.
For more articles about how to convert PHP arrays and strings to each other, refer to PHP Chinese network!