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 converts passed PHP objects into JSON formatted strings. You can control the flow of the conversion by passing optional encoding parameters to the json_encode() function. In this PHP Array to JSON example, we use the json_encode() function to convert 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: 3128 times

What is PHP?

PHP is an open-source server-side scripting language that can be embedded into HTML code. PHP can support multiple databases, including MySQL, PostgreSQL, and SQLite, and can be easily deployed on a wide range of operating systems, including Linux, macOS, and Windows.

What is JSON?

JSON (JavaScript Object Notation) is a language-independent text format for storing and exchanging data. Developers use JSON for web browser-server communication and server-to-server communication via REST API. 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 we use 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?

There are two types of arrays in PHP: regular and associative (an associative array uses a key-value structure to store data). The json_encode() function can 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 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

// try get IpInfo ASAP! getIpInfo($.noop);