如何在 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
廣告