Javatpoint Logo
Javatpoint Logo

Array to string conversion in PHP

While working with PHP, we face many problems related to arrays, where we cannot work on the array directly. To solve it, first we have to convert an array into a string, and then we can efficiently work on a single element.

A string is used to store characters in a sequence and represent it as a single entity with a single data type. Thus, data storage is flexible, making it easy to work with, whereas Array stores data as a particular entity, and the data storage is fixed.

We will be working on three ways to convert array elements into strings in PHP.

1) Implode () function in PHP:

The IMPLODE () function is a built-in PHP function that is mainly used to join all the elements of a declared array. When all the array elements are joined together, they form a string, implode () works similarly to the joint () function in PHP, and return the value as a string.

NOTE: - The array is represented in the form of a string, but the base data type remains array. If we use the gettype () function on the converted string, it will still show an array.

Here the separator refers to what we have to add between elements of an array while converting it to a string. The default value is an empty string "", but we can add many different values like "," "-" "_" "+" ";" ":" etc.

Array stands for the array that will be joined as a string.

Example: To convert an array to string.

When we execute the code as mentioned above, the compiler will give the output -

Array ( [0] = > This [1] = > Array [2] = > Will [3] = > Be [4] = > Converted [5] = > To [6] = > A [7] = > String ) 
converted array 
This Array Will Be Converted To A String
  • In the above code, first, we have declared an array with the name $arra and assigned some values to it. Here each element is considered a separate identity.
  • To show the original, we have used the Print_r function, which displays each array separately.
  • And finally, we have used the implode () function that will convert all the elements of an array into a string. We have initialized both the parameters separator "" to add space between elements and array name to define the array.

Example : To convert an array to string with different parameters.

When we execute the code as mentioned above, the compiler will give the output -

Default array
Array ( [0] => This [1] => Is [2] => An [3] => ARRAY [4] => ! ) 
Array to string
This Is An ARRAY ! 
This _ Is _ An _ ARRAY_ ! 
This - Is - An - ARRAY- ! 
This / Is / An / ARRAY/ ! 
Data type: -
Array
  • In the above code, we first declared an array with the name $arra and assigned some values. Here each element is considered a separate identity.
  • To show the original, we have used the Print_r function, which displays each element of the array separately.
  • And then, we have used the implode () function that will convert all the array elements into a string. Finally, we have initialized different separators to add space between elements.
  • In the end, we have used the gettype () function on our converted array to show that even after conversion to string, the initial data type of the array remains the same.

NOTE: We can again convert the string back to array using the explode () function

Example -> to convert string back to array

When we execute the code as mentioned above, the compiler will give the output -

original string -> we will convert the string back to an array 
converted array -> Array ( [0] = > we [1] = > will [2] = > convert [3] = > string [4] = > back [5] = > to [6] = > array )
  • In the above code, we first declared an array with the name $strng and assigned some values to it.
  • And then, we have used the explode () function that will convert all the string elements back to the array. Finally, we have stored the converted string to a variable $arra.
  • To show the original, we have used the Print_r function, which displays each array separately.

2) Join () function in PHP:

The Join () function is a built-in PHP function commonly used to join arrays and return a string.

A separator refers to what we have to add between elements of an array while converting it to a string. The default value is an empty string "", but we can add many different values like ", " "-" "_" "+" ";" ":" etc.

Array stands for the array that will be joined as a string

Example: To convert an array to string using join.

When we execute the code as mentioned above, the compiler will give the output -

original array 
Array ( [0] = > This [1] = > Array [2] = > Will [3] = > Be [4] = > Converted [5] = > To [6] = > A [7] = > String ) 
converted array 
This Array Will Be Converted To A String 
This _ Array _ Will _ Be _ Converted _ To _ A _ String
  • In the above code, first, we have declared an array with the name $arra and assigned some values to it. Here each element is considered a separate identity.
  • To show the original, we have used the Print_r function, which displays each array separately.
  • And finally, we have used the join () function that will convert all the elements of an array into a string. We have initialized both the parameters separator "" and " _ " to add space between elements and array name to define the array.

3) Json_encode () function in PHP:

The Json_encode () function is a built-in PHP function commonly used to convert arrays and objects in PHP into JSON string (JavaScript Object Notation) representation.

NOTE: - JSON is used commonly to retrieve data from web servers and display the same on web pages

Array/object name stands for the array that will be joined as a string.

Example -> to convert an array to string using json_encode

When we execute the code as mentioned above, the compiler will give the output -

{"f-name":"ABC","l-name":"DEF","0":{"email":"[email protected]","mobile":"abcdefghij","0":{"address":"klmnopqrst","city":"OAB","pincode":"110090"}}}
  • In the above code, we first declared an array with the name $profile and declared a multi-dimensional array inside the variable.
  • After we have declared a variable $json which will be used to hold the return value from
  • And then, we have used the json_encode () function that will convert all the elements of an array into a string.
  • In the end, we have used echo($json) to get the output string.

Next TopicPHP Contact Form





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA