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.
Table of Contents
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:
- 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"
- 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.
$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.
string implode($separator, $array)
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.
string json_encode( $value, $option, $depth )
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
- //assigning value to the array
- $arr = array(“Hello”,“students”, ” “, “how”,“are”,“you”);
- echo implode(” “,$arr);// Use of implode function
- $str=“Hello Students, How are you”;
- //explode function breaks an string into array
- $arr=explode(” “,$str);
Using json() Function
- //Assigning values to the object variable
- //json_encode() put data into associative array with index
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.