【PHP】explode与implode

1.explode — 使用一个字符串分割另一个字符串(字符串转换为数组)

array explode ( string $delimiter , string $string [, int $limit ] )
<?php
  $pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
  $pieces = explode(" ", $pizza);
  echo $pieces[0]; // piece1
  echo $pieces[1]; // piece2
?>
<?php
    $pizza  = "piece1,piece2,piece,piece4,piece5,piece6";
    $pieces = explode(",", $pizza);
    print_r($pieces);
?>

返回结果为:Array ( [0] => piece1 [1] => piece2 [2] => piece [3] => piece4 [4] => piece5 [5] => piece6 )

2.implode — 将一个一维数组的值转化为字符串

<?php
    $pizza  = array("piece1","piece2","piece3");
    $pieces = implode(",", $pizza);
    print_r($pieces);
?>

返回结果:piece1,piece2,piece3

posted on 2017-11-14 18:53  忆华灯纵博  阅读(151)  评论(0编辑  收藏  举报