In this tutorial, you shall learn how to join elements of an array into a string in PHP using implode() function, with example programs.

PHP – Join elements of array

To join elements of an array in PHP, use implode() String function. implode(separator, array) returns a string with the elements or array joined using specified separator.

Examples

1. Join elements of integer array

In the following example, we take an array of numbers, and join the elements of this array, using hyphen - as separator between the elements of array.

PHP Program

</>
Copy
<?php
$arr = array(5, 2, 9, 1);
$output = implode("-", $arr);
printf("Output : %s", $output);
?>

Output

2. Join elements of string array

In the following example, we take an array of strings, and join the elements of this array, using comma , as separator between the elements of array.

PHP Program

</>
Copy
<?php
$arr = array("apple", "banana", "cherry");
$output = implode(",", $arr);
printf("Output : %s", $output);
?>

Output

Conclusion

In this PHP Tutorial, we learned how to join elements of an array into a string, using implode() function, with examples.