我有一个字符串数组,需要构建一个由某个字符(如逗号)分隔的值的字符串。
$tags;
function my_implode($separator,$array){
$temp = '';
foreach($array as $key=>$item){
$temp .= $item;
if($key != sizeof($array)-1){
$temp .= $separator ;
}
}//end of the foreach loop
return $temp;
}//end of the function
$array = array("One", "Two", "Three","Four");
$str = my_implode('-',$array);
echo $str;
使用implode
$array_items = ['one','two','three','four'];
$string_from_array = implode(',', $array_items);
echo $string_from_array;
//output: one,two,three,four
使用join(implode的别名)
$array_items = ['one','two','three','four'];
$string_from_array = join(',', $array_items);
echo $string_from_array;
//output: one,two,three,four