Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP แปลงอาร์เรย์เป็นสตริง

เพื่อแปลงอาร์เรย์เป็นสตริง ให้ใช้ implode() ใน PHP สมมติว่ามีอาร์เรย์ดังนี้

$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

ลองดูตัวอย่างอีกอันที่เพิ่มตัวคั่น (separator)

ตัวอย่างโค้ด

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

ผลลัพธ์

The string is = One*Two*Three