View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
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.
Syntax:
string implode($separator, $array)Example:
PHP
<?php
$arr = array("Welcome","to", "GeeksforGeeks",
"A", "Computer","Science","Portal");
echo implode(" ",$arr);
?>
Output:
Welcome to GeeksforGeeks A Computer Science PortalMethod 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:
Example:
PHP
<?php
$value = array(
"name"=>"GFG",
array(
"email"=>"",
"mobile"=>"XXXXXXXXXX"
)
);
$json = json_encode($value);
echo($json);
?>
Output:
{"name":"GFG","0":{"email":"","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.
This code explains how to convert an array into a string in PHP using implode function.
There are two ways to convert an array into a string in PHP.
- Using implode() function
- Using json_encode() function
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
- implode(separator,array);
Example
- <?php
- $arr = array("Hello","students", " ", "how","are","you");
- echo implode(" ",$arr);
- ?>
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
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
- explode(separator,array,limit);
Example
- <?php
- $str="Hello Students, How are you";
- $arr=explode(" ",$str);
- print_r($arr);
- ?>
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
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
- json_encode(obj_name);
Example
- <?php
- @$myObj->name="Tarun";
- @$myObj->age=20;
- @$myObj->cidy=""Noida";
- $myJSON=json_encode($myObj);
- echo($myJSON);
- ?>
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