How to convert JSON array to string in PHP?

Javascript object notation (JSON) is used as a standardized way to exchange and store data. Data can be formatted as a valid JSON string and then parsed into a

Javascript object

using built in Javascript functions. It is a great way of moving a lot of data from the server-side to the client side (or client to server) while still being easily able to understand and organize is once it has been transmitted especial when making use of

AJAX techniques

.


Converting your PHP array into a valid JSON string


To convert a PHP array into a JSON string, you can make use of the built in function json_encode(). This function takes an array as an argument and outputs a valid JSON string that can then be used in Javascript.


Converting an

associative array

into a JSON string (example):


$testArr = array( "item1" => 1, "item2" => 2, "item3" => 3);     
echo( json_encode( $testArr ) );

This will output the following JSON string:


{"item1":1,"item2":2,"item3":3}     

This string can now be used in Javascript.


As another example, let's create a more complicated

multi-dimensional array

and convert it into a JSON string:


$testArr = array();     
for($i = 0; $i < 3; $i++){     
    $testArr[ "item" . $i ] = array( "item1" => $i + 1, "item2" => $i + 2, "item3" => $i + 3 );     
}     
echo( json_encode( $testArr ) );

This will output the following JSON string:


{"item0":{"item1":1,"item2":2,"item3":3},"item1":{"item1":2,"item2":3,"item3":4},"item2":{"item1":3,"item2":4,"item3":5}}     


You can also include regular

indexed arrays

into your array to be encoded:


$testArr = array( "arr1" => array( 1, 2, 3 ), "arr2" => array( 4, 5, 6 ) );     
echo( json_encode( $testArr ) );

This will output the following JSON string:


{"arr1":[1,2,3],"arr2":[4,5,6]}   


Converting your JSON string into a Javascript object


Once you have acquired your JSON string in Javascript sent from the server through PHP, you can use the JSON.parse() method to convert it into a

Javascript object

from which you can easily access your data.


The JSON.parse() method takes a string as an argument, this string must be a valid JSON string or it will throw an exception. It will return a Javascript object that can then be stored in a variable.


As an example, say we had the following valid JSON string acquired from the server through PHP:


{"item1":1,"item2":2,"item3":3}

To convert it into a Javascript object, we'll use the JSON.parse() method and store the result in a variable from which we'll access the data.


var jsonString = '{"item1":1,"item2":2,"item3":3}'; 
 
var obj = JSON.parse( jsonString ); 
 
alert( obj.item1 + " " + obj.item2 + " " + obj.item3 ); 
 

This will output the following:


1 2 3 


Full example with PHP and Javascript


For this example we'll be creating an associative array in PHP which will be converted into a JSON string, to acquire the string in Javascript we'll use an

Here is a JavaScript on the client, using an AJAX call to request the PHP file from the example above:

Example

Use JSON.parse() to convert the result into a JavaScript object:

const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
  const myObj = JSON.parse(this.responseText);
  document.getElementById("demo").innerHTML = myObj.name;
}
xmlhttp.open("GET", "demo_file.php");
xmlhttp.send();

Try it Yourself »



PHP Array

Arrays in PHP will also be converted into JSON when using the PHP function json_encode():

PHP file

$myArr = array("John", "Mary", "Peter", "Sally");

$myJSON = json_encode($myArr);

echo $myJSON;
?>

Show PHP file »

The Client JavaScript

Here is a JavaScript on the client, using an AJAX call to request the PHP file from the array example above:

Example

Use JSON.parse() to convert the result into a JavaScript array:

var xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
  const myObj = JSON.parse(this.responseText);
  document.getElementById("demo").innerHTML = myObj[2];
}
xmlhttp.open("GET", "demo_file_array.php", true);
xmlhttp.send();

Try it Yourself »


PHP Database

PHP is a server side programming language, and can be used to access a database.

Imagine you have a database on your server, and you want to send a request to it from the client where you ask for the 10 first rows in a table called "customers".

On the client, make a JSON object that describes the numbers of rows you want to return.

Before you send the request to the server, convert the JSON object into a string and send it as a parameter to the url of the PHP page:

How to convert JSON data to string in PHP?

People use json_encode() and json_decode() to convert objects/arrays of PHP to string and back to objects/arrays. Save this answer. Show activity on this post. $array = json_decode($json,true); $string = implode(",",$array);

How to convert an array to string in PHP?

In PHP, the implode() function is a built-in function that takes an array and converts it to a string. implode() doesn't modify the original array. It doesn't matter whether the array is an indexed or associative array. Once you pass in the array to implode() , it joins all the values to a string.

How to convert array to string key value in PHP?

To convert an array to a string, one of the common ways to do that is to use the json_encode() function which is used to returns the JSON representation of a value. This function takes any value as input except resource.

How to convert an array to string?

So how to convert String array to String in java. We can use Arrays. toString method that invoke the toString() method on individual elements and use StringBuilder to create String.