Home > Article > Backend Development > How to convert arrays and strings in PHP (must read)
In PHP, arrays and strings can be converted. Today we will introduce the method of converting arrays and strings. Friends in need can refer to it.
Method introduction:
function implode ($glue = "", array $pieces) {}
Mainly has two parameters
One is the connector (glue: the meaning of glue), the default is an empty string
The other is the array
$test = array("hello","world","php"); echo implode("-",$test);
Result:
hello-world-php
What if it is an array in the form of K-V?
$test = array("h"=>"hello","w"=>"world","p"=>"php"); echo implode("-",$test);
The result is still
hello-world-php
, indicating that it still only affects value.
function explode ($delimiter, $string, $limit = null) {}
explode (explode, maybe explode the string?)
Parameter explanation:
delimiter, delimiter
limit
Document google translation results:
If limit is set to positive, the returned array will Contains the maximum limiting element, with the last element containing the remainder of the string.
Personal understanding:
limit limits the number of split parts. The last part is the remaining string after splitting. Of course, if it is 1, it is the string itself (has passed Experiment)
Code demonstration:
$str="hello-world-php"; $result = explode("-", $str); var_dump($result); $result = explode("-", $str,2); var_dump($result);
Output result:
array(3) { [0]=> string(5) "hello" [1]=> string(5) "world" [2]=> string(3) "php" } array(2) { [0]=> string(5) "hello" [1]=> string(9) "world-php" }
Maybe the string does not need to be divided, but each one needs to be Take out the characters, that is, read them out one by one
Original document:
** * Convert a string to an array * @link http://php.net/manual/en/function.str-split.php * @param string $string <p> * The input string. * </p> * @param int $split_length [optional] <p> * Maximum length of the chunk. * </p> * @return array If the optional split_length parameter is * specified, the returned array will be broken down into chunks with each * being split_length in length, otherwise each chunk * will be one character in length. * </p> * <p> * false is returned if split_length is less than 1. * If the split_length length exceeds the length of * string, the entire string is returned as the first * (and only) array element. * @since 5.0 */ function str_split ($string, $split_length = 1) {}
Partial translation:
Array If the optional split_length parameter is specified, the returned array will is split into chunks of length split_length, otherwise each chunk would be one character long.
Convert a string to an array, isn’t this just converting a string into an array, there is nothing to say about the usage
Now I want to give it a try, if it is 5 characters in length The string is read in two digits. If there are less than two digits, should the last digit be retained?
Of course, presumably for the sake of data integrity, it should be retained.
$str = "hello"; var_dump(str_split($str,2));
The result is just as I expected
array(3) { [0]=> string(2) "he" [1]=> string(2) "ll" [2]=> string(1) "o" }
Recommended learning: php video tutorial
The above is the detailed content of How to convert arrays and strings in PHP (must read). For more information, please follow other related articles on the PHP Chinese website!