In php learning, strings and arrays are two very important knowledge. Conversion between strings and arrays is frequently used in development. It is mainly implemented through The explode () function and implode () function.
In php learning, strings and arrays are two very important knowledge. Conversion between strings and arrays is frequently used in development. It is mainly implemented through The explode () function and implode () function.
1. convert the string into an array.
Explode (string separator, string, [int limit]) returns an array converted from a string. each element is a substring of a string, which is separated by the string separator.
If the limit parameter is set, the returned array contains up to limit elements, and the last few elements will contain the rest of the string.
If the separator is a null string "", The explode function returns false. if the value contained in the separator cannot be found in the string, The explode function returns an array of a single string element; if the limit value is negative, all elements except the last-limit element are returned.
For example:
$ Str = "messi, henry, xavi ";
$ Array = explode (",", $ str );
Var_dump ($ array );
2. convert an array to a string
Implode (string glue, array pieces)
The glue parameter is a string type and a delimiter. the pieces parameter is an array type to be converted.
For example:
$ Array = array ("team" => "barcelona", "name" => "messi ");
$ Str = implode (",", $ array );