Home  >  Article  >  php教程  >  PHP learning string and array conversion

PHP learning string and array conversion

黄舟
黄舟Original
2016-12-20 11:49:071123browse

In PHP learning, strings and arrays are two very important pieces of knowledge. Conversion between strings and arrays is often used in development. It is mainly implemented through the explode() function and implode() function.
1. Convert string to array.
explode(string separator, string string, [int limit]) returns an array converted from string. Each element is a substring of string, and they are separated by string separator.
If the limit parameter is set, the returned array contains up to limit elements, and the last few elements will contain the remainder of the string.
If the separator is an empty string "", the explode function will return false; if the value contained in the separator is not found in the string, the explode function will return an array of single elements of the string; if the limit is a negative number, it will return except the last All elements except -limit elements.
For example:
$str = "messi,henry,xavi";
$array = explode(",",$str);
var_dump($array);
2. Convert the array into a string
implode(string glue, array pieces)
The parameter glue is a string type, which refers to the delimiter, and the parameter pieces is the array type to be converted.
For example:
$array = array("team"=>"barcelona","name"=>"messi");
$str = implode(",",$array);

The above is PHP learning Regarding the conversion of strings and arrays, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


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