php中字符串与数组的相互转化explode(separator,$str)与implode(separator,$arr)

来源:互联网 发布:银行卡图片制作软件 编辑:程序博客网 时间:2024/06/05 11:25


分割字符串转化为数组

exlpode(separator,str,limit);

limit是可选的,>0,包含最多limit可元素。

  • 大于 0 - 返回包含最多 limit 个元素的数组
  • 小于 0 - 返回包含除了最后的 -limit 个元素以外的所有元素的数组
  • 0 - 返回包含一个元素的数组
eg:

<?php
$str="one two three four";
$arr_new=explode(" ",$str,2);
print_r($arr_new);
?>

结果为

Array ( [0] => one [1] => two three four )


<?php
$str="one two three four";
$arr_new=explode(" ",$str,-1);
print_r($arr_new);
?>

Array ( [0] => one [1] => two [2] => three )



对于implode("",$arr)把数组转化为字符串,separator默认为“”;




0 0
原创粉丝点击