php 将数组转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5237211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:42:50 来源:igfitidea点击:
Convert array to string
提问by Captain Comic
I have an array of strings and I need to build a string of values separated by some character like comma
我有一个字符串数组,我需要构建一个由逗号等字符分隔的值字符串
$tags;
回答by Sean Walsh
回答by Vijay Verma
If any one do not want to use implode so you can also use following function:
如果有人不想使用内爆,那么您也可以使用以下功能:
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;
回答by Joyal
Using implode
使用内爆
$array_items = ['one','two','three','four'];
$string_from_array = implode(',', $array_items);
echo $string_from_array;
//output: one,two,three,four
Using join(alias of implode)
使用join(内爆的别名)
$array_items = ['one','two','three','four'];
$string_from_array = join(',', $array_items);
echo $string_from_array;
//output: one,two,three,four