JSON has become a standard format for sharing data across APIs, web apps, and modern software systems. PHP makes it easy to transform arrays into JSON strings with the native json_encode() function.

This guide covers how to convert PHP arrays into JSON, explores different encoding settings, and explains the behavior of JSON_FORCE_OBJECT with practical examples.

What is JSON?

JSON (JavaScript Object Notation) is a simple way to store and share data between different applications. It is easy for people to read and write, and also easy for computers to understand.

JSON uses key-value pairs (like name: "John") and lists (arrays) to organize data. Because of this simple structure, it is widely used in APIs, websites, and configuration files.

Example of JSON data:

{
  "name": "Mohan",
  "age": 35,
  "city": "Mumbai",
  "language": ["Hindi", "English", "Marathi"]
}

PHP arrays can easily be transformed into this format for APIs, AJAX responses, and database-driven applications.

Convert Array to JSON in PHP

PHP includes the json_encode() function, which is used to transform arrays and objects into JSON format.

Syntax:

json_encode($value, $flags, $depth);

Parameters

  • $value: The array or object that will be converted into JSON
  • $flags: Optional constants used to modify the encoding output
  • $depth: Sets the maximum depth allowed during encoding

Example 1: Converting an Indexed Array into JSON Format

<?php

$cities = ["Patna", "Patiala", "Bhatinda"];

$json = json_encode($cities);

echo $json;

?>

Output:

["Patna","Patiala","Bhatinda"]

In this example, the indexed array is converted into a JSON array structure.

Example 2: Converting an Associative Array into JSON Format

<?php

$user = [
    "name" => "mohan",
    "email" => "mohan@example.com",
    "age" => 35,
    "city"=> "Mumbai",
    "language"=> ["Hindi", "English", "Marathi"]
];

echo json_encode($user);

?>

Output:

{"name":"mohan","email":"mohan@example.com","age":35,"city":"Mumbai","language":["Hindi","English","Marathi"]}

In this example, associative arrays are converted into JSON objects.

Formatted JSON Output (Pretty Print)

Human-readable JSON is especially helpful when debugging or during development.

<?php

$user = [
    "name" => "mohan",
    "email" => "mohan@example.com",
    "age" => 35,
    "city"=> "Mumbai",
    "language"=> ["Hindi", "English", "Marathi"]
];

echo json_encode($user, JSON_PRETTY_PRINT);

?>

Output:

{
    "name": "mohan",
    "email": "mohan@example.com",
    "age": 35,
    "city": "Mumbai",
    "language": [
        "Hindi",
        "English",
        "Marathi"
    ]
}

 

What is JSON_FORCE_OBJECT in PHP?

JSON_FORCE_OBJECT is a flag used with json_encode() that forces PHP arrays to be encoded as JSON objects instead of JSON arrays.

Normally, PHP converts indexed arrays into JSON arrays, but when JSON_FORCE_OBJECT is used, the same array is encoded as an object with numeric keys

This is especially useful when:

  • You want consistent object formatting
  • APIs require object responses
  • Empty arrays should return {} instead of []

Example of JSON_FORCE_OBJECT

<?php

$languages = ["Hindi", "English", "Marathi"];

echo json_encode($languages, JSON_FORCE_OBJECT);

?>

Output:

{"0":"Hindi","1":"English","2":"Marathi"}

Normally, indexed arrays become JSON arrays:

["Hindi","English","Marathi"]

But JSON_FORCE_OBJECT converts them into JSON objects.

Convert Empty Array to JSON Object

Without JSON_FORCE_OBJECT:

<?php

echo json_encode([]);

?>

Output:

[]

Using JSON_FORCE_OBJECT:

<?php

echo json_encode([], JSON_FORCE_OBJECT);

?>

Output:

{}

This is helpful in APIs that require empty objects rather than empty arrays.

Combining Multiple JSON Flags

You can combine multiple flags using the bitwise OR operator (|).

<?php

$user = [
    "name" => "mohan",
    "email" => "mohan@example.com",
    "age" => 35,
    "city"=> "Mumbai",
    "language"=> ["Hindi", "English", "Marathi"]
];

echo json_encode(
    $user, 
    JSON_FORCE_OBJECT | JSON_PRETTY_PRINT
);

?>

Output:

{
    "name": "mohan",
    "email": "mohan@example.com",
    "age": 35,
    "city": "Mumbai",
    "language": {
        "0": "Hindi",
        "1": "English",
        "2": "Marathi"
    }
}

Common JSON Encoding Flags in PHP

  • JSON_PRETTY_PRINT: Formats JSON output for better readability
  • JSON_FORCE_OBJECT: Converts arrays into JSON objects
  • JSON_UNESCAPED_UNICODE: Keeps Unicode characters without escaping
  • JSON_UNESCAPED_SLASHES: Prevents escaping of forward slashes
  • JSON_NUMERIC_CHECK: Converts numeric strings into actual numbers

Difference Between JSON Array and JSON Object

JSON Array

  • Uses square brackets []
  • Ordered list of values
  • Example: ["A","B"]

JSON Object

  • Uses curly braces {}
  • Key-value pairs
  • Example: {"0":"A","1":"B"}