In PHP programming, we usually encounter the situation of converting arrays into strings. The most common one is that when passing data to the front end through the API, the data needs to be converted into a string format and passed. So, how to convert an array into a string and split it in PHP? This article will introduce in detail the method of splitting an array into a string.
The implode function is a commonly used array to string function in PHP. It can connect the elements in an array into a string.
The function syntax is as follows:
implode ( string $glue , array $pieces ) : string
Among them, $glue represents the delimiter of the connection string, and $pieces represents the array to be connected.
The following is a sample code:
$arr = array('apple','banana','orange'); $delimiter = ','; $str = implode($delimiter, $arr); echo $str;
In the above code, we convert the array $arr into a comma-separated string, and then output the value of the string $str on the screen, That is:
apple,banana,orange
We can modify the delimiter in the $delimiter parameter, such as using "-":
$arr = array('apple','banana','orange'); $delimiter = '-'; $str = implode($delimiter, $arr); echo $str;
Output result:
apple-banana-orange
When using the implode function, you need Note the following:
The join function has exactly the same function as the implode function, both convert the elements in the array into strings. It's just that the parameter order of the join function is opposite to that of the implode function.
The function syntax is as follows:
join ( string $glue , array $pieces ) : string
The sample code is as follows:
$arr = array('apple','banana','orange'); $delimiter = ','; $str = join($delimiter, $arr); echo $str;
The output result is the same as the implode function:
apple,banana,orange
In some cases, we need to split the string into an array.
PHP provides the explode function, which has the opposite effect to the implode function and can split a string into an array by specifying the delimiter.
The function syntax is as follows:
explode ( string $delimiter , string $string , int $limit = PHP_INT_MAX ) : array
Among them, $delimiter represents the delimiter of the string, $string represents the string to be split, and $limit represents the maximum length of the array after splitting.
The sample code is as follows:
$str = 'apple,banana,orange'; $delimiter = ','; $arr = explode($delimiter, $str); print_r($arr);
Output result:
Array ( [0] => apple [1] => banana [2] => orange )
When using the explode function, you need to pay attention to the following points:
In this article, we introduce the method of converting array to string segmentation in PHP. Whether you use the implode function to convert an array into a string, or use the explode function to divide a string into an array, I believe these methods can help you better complete your PHP programming tasks.
The above is the detailed content of How to convert php array to string segmentation. For more information, please follow other related articles on the PHP Chinese website!