Home > Article > Backend Development > The use of explode and implode in PHP
##explode() function splits the string into arrays;
implode() function splits the array into The elements are combined into a string.
$str = "hi, jason, world"; print_r(explode(',', $str)); print_r(explode(',', $str, 2));
Result:
Array ( [0] => hi [1] => jason [2] => world ) Array ( [0] => hi [1] => jason, world )Exercise example: Define a string a, calculate how much the values after the underscore add up, and use the split string function.
$a='331612_1,331495_1,331461_2,331559_1,333080_1,333555_1,331448_1,331618_1,333388_1,33 3408_1,327937_1,331615_1,331499_1'; $arr=explode(',',$a); $sum=0; foreach ($arr as $value) { $ary=explode('_', $value); $sum+=$ary[1]; } print_r($sum);
<?php $array = array( 'lastname' , 'email' , 'phone' ); $comma_separated = implode ( "," , $array ); echo $comma_separated ; // lastname,email,phoneFor more PHP related knowledge, please visit
PHP Chinese website!
The above is the detailed content of The use of explode and implode in PHP. For more information, please follow other related articles on the PHP Chinese website!