如何在 PHP 中將陣列轉換為字串?


要將陣列轉換為字串,可在 PHP 中使用 implode () 的概念。假設我們有如下陣列 -

$sentence = array('My','Name','is','John');

要將以上陣列轉換為字串 -

,implode(" ",$sentence)

示例

 線上演示

<!DOCTYPE html>
<html>
<body>
<?php
   $sentence = array('My','Name','is','John');
   echo "The string is=",implode(" ",$sentence);
?>
</body>
</html>

輸出

The string is = My Name is John

現在,我們來看一個 PHP 程式碼,在這個程式碼中,我們也會新增分隔符 -

示例

 線上演示

<!DOCTYPE html>
<html>
<body>
<?php
   $sentence = array('One','Two','Three');
   echo "The string is=",implode("*",$sentence);
?>
</body>
</html>

輸出

The string is = One*Two*Three

更新於: 2020 年 10 月 12 日

302 次瀏覽

開啟你的 職業

透過完成該課程獲得認證

開始
廣告