August 17, 2024
PHP implode()
implode() function is one of the most important PHP's built in function of array. Sometimes it happened that you want to convert an array's elements into a string so for the same you can use implode function in PHP. implode function requires two string arguments first is a separator string and second array from which you will create the string. The implode function convert each element of an array into a string. implode() function attach separator string after each element of the array. The example of implode function is mentioned below:
Code
<?php
$weekDaysArray[] = "Sunday";
$weekDaysArray[] = "Monday";
$weekDaysArray[] = "Tuesday";
$weekDaysArray[] = "Wednesday";
$weekDaysArray[] = "Thursday";
$weekDaysArray[] = "Friday";
$weekDaysArray[] = "Saturday";
$weekDaysString = implode(",",$weekDaysArray);
print($weekDaysString);
?>
In above mentioned example we have an array with the name of $weekDaysArray which have all the week days. In line number 9 we have used implode function with $weekDaysArray array and provide separator string as comma(,) then implode function returns a string of all the week days and we store it in $weekDaysString variable and print it on the screen through print function.