Converting PHP Array to JSON

To convert a PHP array to JSON data string, you can use the json_encode($value, $flags, $depth) function. The json_encode() function takes a PHP array as input and returns a JSON string representation. You can customize the conversion of a PHP array to JSON using additional parameters for the json_encode() function, such as "JSON_PRETTY_PRINT", for human-readable formatting and JSON output behavior. In this PHP Array to JSON example, we use the json_encode() function to convert a PHP array to a JSON string with no additional parameters. Below, you'll find more examples demonstrating additional options for converting a PHP array to a JSON string. Click Execute to run the Convert PHP Array to JSON String Example online and see the result.
Converting PHP Array to JSON Execute
<?php 
$arr = array(
  'US' => 'Washington',  
  'UK' => 'London',
  'Spain' => 'Madrid', 
  'Italy' => 'Rome'
); 

echo json_encode($arr);
?>
Updated: Viewed: 11544 times

What is PHP?

PHP is an open-source server-side scripting language that can be embedded into HTML code. PHP supports multiple databases, including MySQL, PostgreSQL, and SQLite, and runs on various operating systems such as Linux, Windows, macOS, and Unix.

What is JSON?

JSON (JavaScript Object Notation) is a language-independent text format for storing and exchanging data. Developers use JSON in web server communications that require the exchange of structured data between a client and a server. Many programming languages have built-in or external libraries for creating and manipulating JSON data strings, including PHP, JavaScript, Java, C++, C#, Go, and Python. JSON file names use the .json extension.

What is an array in PHP?

In PHP, an array is a variable that is used to store data containing various elements. The elements of an array are identified by a unique key or index, which can be a number or a string. PHP has two types of arrays: indexed arrays and associative arrays. PHP provides many built-in functions for working with arrays, including searching, sorting, splitting a string to array, converting an array to string, getting the length of an array, and converting an array to JSON.

How to encode a PHP array into a JSON string?

To encode a PHP array into a JSON string, you can use json_encode() method to convert both array types to a JSON data string. The following is an example of converting an associative array to a JSON string:

Convert PHP Array to JSON Example
<?php
$arr = array(
  'US' => 'Washington',  
  'UK' => 'London',
  'Spain' => 'Madrid', 
  'Italy' => 'Rome'
); 

echo json_encode($arr);
?>

#output: {"US":"Washington","UK":"London","Spain":"Madrid","Italy":"Rome"}

Converting PHP Array to JSON Examples

The following are examples of converting a PHP array to JSON:

Converting an array to a pretty JSON

The following is an example of converting an array to beautiful JSON using the json_encode() method:

PHP Convert Array to Pretty JSON Example
<?php
$arr = array(
  'US' => 'Washington',  
  'UK' => 'London',
  'Spain' => 'Madrid', 
  'Italy' => 'Rome'
); 

echo json_encode($arr, JSON_PRETTY_PRINT);
?>

#output: 
# {
#   "US": "Washington",
#   "UK": "London",
#   "Spain": "Madrid",
#   "Italy": "Rome"
# }

Converting an array to JSON

The following is an example of converting an array to JSON using the json_encode() method:

PHP Convert Array to JSON Example
<?php
$arr = array('PHP', 'Array', 'to', 'JSON');

echo json_encode($arr);
?>

#output: ["PHP","Array","to","JSON"]

Converting an associative array to JSON

The following is an example of converting an associative array to JSON using the json_encode() method:

PHP Convert Associative Array to JSON Example
<?php
$arr = array('name' => 'Leo', 'age' => 26, 'city' => 'London');

echo json_encode($arr);
?>

#output: {"name":"Leo","age":26,"city":"London"}

Converting a multidimensional associative array to JSON

The following is an example of converting a multidimensional associative array to JSON using the json_encode() function:

PHP Convert Multidimensional Array to JSON Example
<?php
$arr = array (
  'first"' => array(
    'id'=> 1,
    'product' => 'Pineapple',
    'price' => 90
  ),
  'second' => array(
    'id' => 2,
    'product' => 'Orange',
    'price' => 70
  )
);

echo json_encode($arr);
?>

#output: {"first\"":{"id":1,"product":"Pineapple","price":90},"second":{"id":2,"product":"Orange","price":70}}

See also