Array to string
With PHP you can convert an array into a string.
To do that we have the implode()
function and for historical and retro-compatibility reasons we have also the join()
function. The join()
function is an alias for implode()
. So from the developer perspective, there are no differences in terms of result calling implode()
or calling join()
.
A string from an array: implode()
and/or join()
If you need to create a string starting from an array you can use the implode()
(or join()
) function.
For example:
- you have an array
["a", "b" , "c"]
and you want to obtain"abc"
; - you have an array
["a", "b" , "c"]
and you want to obtain"a-b-c"
(with โ-โ as the separator); Theimplode()
(orjoin()
) function concatenates each element of the array using the string separator. The elements may be string, character, number, or boolean. The separator may be a string (or just one character).
If you have an array of characters and you want to obtain a string with โ-โ as the separator:
You can also use a longer string as the separator:
If you have an array of strings and you want to obtain a string with โ โ as the separator:
You can join not just strings but numbers and booleans as well:
You can omit the separator to concatenate the elements of the array, the returned string is a string with the array elements concatenated with no separator character: