How to convert array to 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 array is a data structure used to store one or more similar types of values in a single variable. Array in PHP is also known as an ordered map.

In PHP, sometimes, it is asked to convert an Array to a String. PHP offers built-in methods to convert Array to a String. We can decide any of these methods for this task according to our preferences.

This article will explain two methods to convert Array to String in PHP. There will be example codes along with the output for better understanding.

Following are the two inbuilt functions of PHP to convert an Array into a String.

We have given an array and the task is to convert the array elements into string. In this article, we are using two methods to convert array to string.

Method 1: Using implode() function: The implode() method is an inbuilt function in PHP and is used to join the elements of an array. The implode() method is an alias for PHP | join() function and works exactly same as that of join() function.

Convert PHP Array to String using implode()

PHP comes with its native function to convert Array to string called implode().

The implode() function has two implementation:

  1. The implode() function that accepts two arguments – String used to join array elements & Array itself

    Example: Here we will join array elements using “, ” to store it as comma separated values.

    $arr = ['Thor','Captain America','Iron Man'];
    echo implode(', ',$arr);
    // "Thor, Captain America, Iron Man"
  2. The implode() function that accepts only one argument – Array to be converted

    Here, the string to be used to join elements defaults to empty string. Therefore, elements are joined together without any separator.

    Example:

    $arr = ['H','e','l','l','o'];
    echo implode($arr);
    // "Hello"

Note: If the PHP array contains non-string items, implode function will first convert it to string and use it in the joined string. Boolean values are representated as → true = 1, false = 0.

Syntax:

string implode($separator, $array)

Example:

<?php  
 
// Declare an array 
$arr = array("Welcome","to", "GeeksforGeeks"
    "A", "Computer","Science","Portal");  
   
// Converting array elements into
// strings using implode function
echo implode(" ",$arr);
  
?>

Output:

Welcome to GeeksforGeeks A Computer Science Portal

Method 2: Using json_encode() Function: The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation.
Syntax:

string json_encode( $value, $option, $depth )

Example:

<?php 
 
// Declare multi-dimensional array 
$value = array
    "name"=>"GFG"
    array
        "email"=>"[email protected]"
        "mobile"=>"XXXXXXXXXX"
    
); 
 
// Use json_encode() function 
$json = json_encode($value); 
 
// Display the output 
echo($json); 
 
?> 

Output:

{"name":"GFG","0":{"email":"[email protected]","mobile":"XXXXXXXXXX"}}

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

Using implode() Function

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.
Syntax
  1. implode(separator,array);

Example

  1. <?php
  2. //assigning value to the array
  3. $arr = array(“Hello”,“students”” ““how”,“are”,“you”);
  4. echo implode(” “,$arr);// Use of implode function
  5. ?>
In the above example, at first line, an array variable is declared and assigned some values.
In the next line, the implode() function will convert the array into a string. Two parameters are passed in the implode() function. First is the separator and the other one is array_name.
Output
Implode Function
You can also convert that string into an array if required. For that, we can use PHP’s explode() function.

explode() function

By using explode() function, we can convert a string into array elements. We can pass three arguments in it. The first one is for separator, second for array_name, and the last one for the limit.
Syntax
  1. explode(separator,array,limit);
Example
  1. <?php
  2. $str=“Hello Students, How are you”;
  3. //explode function breaks an string into array
  4. $arr=explode(” “,$str);
  5. print_r($arr);
  6. ?>
In the above example, a string variable is assigned some value. Then, explode() function breaks this string into an array. After this, we used print_r() function which prints all the array elements and its indexes.
Output
explode function

Using json() Function

In PHP, objects can be converted into JSON String by using the PHP function json_encode().
A common use of JSON is to read data from a web server and display the data on a web page.
Syntax
  1. json_encode(obj_name);
Example
  1. <?php
  2. //Assigning values to the object variable
  3. @$myObj->name=“Tarun”;
  4. @$myObj->age=20;
  5. @$myObj->cidy=“”Noida”;
  6. //json_encode() put data into associative array with index
  7. $myJSON=json_encode($myObj);
  8. echo($myJSON);
  9. ?>

In the above example, we assigned the value to the object variable and then in json_encode()converts the value to the array variable and makes the associative array.

Output
json_encode function

Leave a Reply

Your email address will not be published. Required fields are marked *