2012-07-14 160 views
3

我記得有一個簡單的方法,沒有爲每個重建一個數組成一個字符串。從數組創建字符串?

EG:(在與之間空格)

1 > my 
2 > name 
3 > is 
4 > sam 

$string = "my name is sam"

這是怎麼用PHP完成的?

回答

8

你只需要implode字符串:

$array = array("my", "name", "is", "sam"); 
$string = implode(' ', $array); 
echo $string; // "my name is sam" 
3

試試這個:

<?php 

$array = array('lastname', 'email', 'phone'); 
$comma_separated = implode(",", $array); 

print $comma_separated; // lastname,email,phone 

?> 
+0

或者打印一個由行:'$ one_per_line =破滅(PHP_EOL,$陣列);' – wranvaud 2017-07-27 00:15:40