Encoding PHP Object to JSON String

To encode a PHP object to a JSON formatted string, you can use the json_encode($value, $options, $depth) function. The $value parameter specifies the PHP object to encode. The additional $options parameter provides additional options for JSON encoding. You can control how the PHP object will be encoded into JSON by passing a combination of bitmasks. See the list of supported bitmasks for JSON encoding below. The third optional parameter specifies the maximum recursion depth, in other words, how deep the encoding function will go through the nested PHP objects and convert them to JSON. In this PHP Encode JSON example, we use the json_encode() function to encode a PHP object into its JSON representation. Click Execute to run the PHP JSON Encode Example online and see the result.
Encoding PHP Object to JSON String Execute
<?php
$json = array(
  'Leo' => 35,
  'Alice' => 37,
  'Dora' => 43
);

echo json_encode($json);
?>
Updated: Viewed: 10775 times

What is PHP?

PHP is a general-purpose server-side programming language for web development. PHP integrates with many web development technologies, such as HTML, CSS, JavaScript, and SQL. PHP can run on many operating systems, including Windows, Linux, and macOS, and supports multiple databases such as MySQL, PostgreSQL, and SQLite.

What is JSON?

JavaScript Object Notation (JSON) is a textual format for representing structured data based on the syntax of a JavaScript object. JSON is widely used for data exchange between applications written in different programming languages, including PHP, JavaScript, Java, C++, C#, Go, Python, and many more. JSON file names use the .json file extension.

How to encode an object into a JSON string in PHP?

To convert a PHP array or object to a JSON representation, you can use the json_encode() function:

PHP json_encode() Syntax
json_encode(value, options, depth)

Where:
  • value: required parameter, specifies the value to encode
  • options (Optional): specifies a bitmask that determines how JSON objects will be converted to PHP objects. The encode function supports the following bitmasks: JSON_FORCE_OBJECT, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, etc. You can learn more about JSON encoding bitmasks in the PHP documentation.
  • depth (Optional): defines the maximum depth. The value must be greater than zero
PHP json_encode() Example
<?php
$json = array(
  'Leo' => 35,
  'Alice' => 37,
  'Dora' => 43
);

echo json_encode($json);
?>

#output: {"Leo":35,"Alice":37,"Dora":43}

PHP JSON Encode Examples

The following are examples of using json_encode() function in PHP:

Encoding an associative array to JSON

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

PHP Encoding Associative Array to JSON Example
<?php
$data = array(
  'name' => 'Leo',
  'age' => 26,
  'city' => 'Madrid'
);

echo json_encode($data);
?>

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

Encoding multidimensional array to JSON

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

PHP Encoding Multidimensional Array to JSON Example
<?php
$data = array(
  'user' => array(
    'name' => 'Leo',
    'age' => 26,
  ),
  'orders' => array(
    array('id' => 1, 'rating' => 'VIP'),
  )
);

echo json_encode($data);
?>

#output: {"user":{"name":"Leo","age":26},"orders":[{"id":1,"rating":"VIP"}]}

Encoding an object to JSON

The following is an example of encoding an object to JSON using the json_encode() function:

PHP Encoding Object to JSON Example
<?php
class User {
  public $name;
  public $age;
  public $city;
  function __construct($name, $age, $city) {
    $this->name = $name;
    $this->age = $age;
    $this->city = $city;
  }
}
$user = new User('Leo', 26, 'Madrid');

echo json_encode($user);
?>

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

Encoding an UTF-8 string to JSON

The following is an example of encoding a UTF-8 string to JSON using the json_encode() function:

PHP Encoding UTF-8 String to JSON Example
<?php
$str = "Héllo, Wörld!";

echo json_encode($str, JSON_UNESCAPED_UNICODE);
?>

#output: "Héllo, Wörld!"

How to decode JSON string into PHP object?

To decode a JSON string back into a PHP object, you can use the json_decode() function. The result object can be any PHP data type, including an array, except resource pointers such as a database or file descriptor.

JSON to PHP Array Converter Example
<?php
$json = '{"Id": "Leo", "Age": "26", "City": "Madrid"}';

print_r (json_decode($json));
?>

#output:
# stdClass Object
# (
#   [Id] => Leo
#   [Age] => 26
#   [City] => Madrid
# )

See also