How to Convert array into string in PHP

How to Convert array into string in PHP

How to Convert array into string in PHP

While working with PHP, many times the developer requires converting the array data into string so they can easily implement the String functions to the data.

In this tutorial, we will briefly discover the various ways of converting Array into String in PHP.

PHP provides different inbuilt functions to help the programmers for converting an Array to String. 

– Both the built-in functions only take one array at a single time and automatically convert the array data into a string.

  • PHP json_encode() function
  • PHP implode() function

1 - PHP json_encode() function

In PHP, the json_encode() is the most commonly used function to convert an array to a string. This function returns the JSON value of the given array. 

The json_encode() function accepts an element as input except the resource values.

Syntax:

json_encode ( mixed$value [, int $options = 0 [, int $depth = 512 ]] ) : string|false

Example:

In the below example, we have taken a multidimensional Array as input data. Then we have applied the json_encode() function to convert it into a string or a JSON value of the given array.

<?php  
$electronics = array(  
  array(  
    'Samsung' => 'South Korea'  
  ),  
  array(  
    'Tata' => 'India'  
  ),  
  array(  
    'Apple' => 'American'  
  )  
);  
//applying the json_encode() function to convert  
//an array to string  
echo json_encode($electronics);  

Output:

[{"BMW":"Germany"},{"Ferrari":"Italy"},{"Honda":"Japan"}]

– Though, if you look at the output, it doesn't look like a string, but that's how JSON's output looks. Further, if the programmer use the var_dump() function over json_encode() then it will display the values as a string.

2 - PHP implode() function to convert an array to a string

The PHP implode() function accepts the input as an array and further converts it to a string. This function uses the glue parameter, which also acts as the separator for the given array argument. Therefore this function accepts the array, converts its values into a string, and concatenates them using a separator.

Syntax:

implode ( string $glue , array $pieces ) : string

Parameters:

$glue: This parameter accepts string values / special character(s) that are further used to concatenate array values. By default it accepts an empty string.

$pieces: This parameter represents an array whose values stick together using glue.

Return:

The PHP implode() function will return all the Array values that are joined together using the glue in the exact sequential order in which they were present in the given array.

Example: 

Below given is the demonstration of code for converting array into string using implode() function.

<!DOCTYPE html>  
<html>  
<body>  
<?php  
   $sentence = array('Welcome','to','XDevSpace');  
   //converting the array to String with the help of PHP implode() function  
   echo "The converted string is=",implode(" ",$sentence);  
?>  
</body>  
</html>  

Output:

The converted string is = Welcome to XDevSpace
All Comments