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.
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
<?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
<?php // Declare multi-dimensional array $value = array ( "name" => "GFG" , array ( "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
- implode(separator,array);
Example
- <?php
- //assigning value to the array
- $arr = array(“Hello”,“students”, ” “, “how”,“are”,“you”);
- echo implode(” “,$arr);// Use of implode function
- ?>
explode() function
- explode(separator,array,limit);
- <?php
- $str=“Hello Students, How are you”;
- //explode function breaks an string into array
- $arr=explode(” “,$str);
- print_r($arr);
- ?>
Using json() Function
- json_encode(obj_name);
- <?php
- //Assigning values to the object variable
- @$myObj->name=“Tarun”;
- @$myObj->age=20;
- @$myObj->cidy=“”Noida”;
- //json_encode() put data into associative array with index
- $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.