Php Array To String


The Answer: Using implode()

  • You have an integer and you want to change its type to string. Solution: There are few ways to change the type of a value from integer to string. In the following you’ll find three ways-Method 1: Applying type casting In this method, to convert an integer to string, write (string) before the integer, and PHP will convert it to string type.
  • The optional input parameter limit is used to signify the number of elements into which the string should be divided, starting from the left end of the string and working rightward. In cases where the pattern is an alphabetical character, split is case sensitive. Returns an array of strings after splitting up a string.
Php array to string

By using the implode function, we can convert all array elements into a string. This function returns the string. The separator parameter in implode function is optional. But it is good to use two arguments.

As I’ve always said, PHP has the answer for everything, and the conversion of an array to a flat string is no exception.

Using implode to convert an array to a string

The really great thing about this conversion is it’s a one-liner function call.

The implode function takes two parameters,

  • Glue (string), a specified string that will essentially be concatenated to each of the array values (Sticking the values together). Although note that the Glue (what I like to call a delimiter) doesn’t have to be used, an array on its own can be passed in but the string representation will print without anything in between the values,
  • Pieces (array), the array we want to convert to a string.

In the following 2 examples, notice the Glue is a string of white space which will print a space between each of the array values when we echo it.

Example 1


CodeWall Check out our latest post on CodeWall 2020-05-29 10:40:23 6 9

Example 2

Php Explode


Php Array To String Implode

one two three

Surfshark windows 10

Example 3

Php Array To String With Quotes

In this particular example, notice the glue parameter has been removed, leaving the function to convert the array to a string without any delimiter.

Php Array To String With Commas