In-depth Analysis of json_encode in PHP: Encoding Arrays as JSON Arrays vs. Objects

Dec 01, 2025 · Programming · 24 views · 7.8

Keywords: PHP | json_encode | JSON array | array_values | RFC 8259

Abstract: This article explores why the json_encode function in PHP sometimes encodes arrays as JSON objects instead of arrays. The key factor is the continuity of array keys. By analyzing the RFC 8259 standard, it explains the differences between JSON arrays and objects, and provides a solution: using the array_values function to reindex arrays. The article also discusses the distinction between HTML tags like <br> and characters like \n, ensuring code examples are clear and accessible.

Introduction

In PHP development, the json_encode function is commonly used to convert arrays to JSON format, but developers often encounter confusion: why are some PHP arrays encoded as JSON objects instead of the expected JSON arrays? Based on the RFC 8259 standard and PHP official documentation, this article provides an in-depth analysis of this phenomenon and offers practical solutions.

Differences Between JSON Arrays and Objects

According to RFC 8259, a JSON array is defined as zero or more values enclosed in square brackets, with elements separated by commas. For example, ["value1", "value2"] is a valid JSON array. In contrast, a JSON object is enclosed in curly braces and contains key-value pairs, with keys as strings. For example, {"key": "value"}. In JavaScript, arrays are indexed collections, while objects are key-value mappings, which influences JSON encoding behavior.

Encoding Behavior of PHP Arrays

The json_encode function determines whether to encode a PHP array as a JSON array or object based on its keys. If the array keys are consecutive numbers (e.g., 0, 1, 2...), it encodes as a JSON array; otherwise, it encodes as a JSON object. For example, consider the following PHP array:

$array = array(
    0 => array("id" => 0, "name" => "name1"),
    2 => array("id" => 2, "name" => "name2")
);

Calling json_encode($array) outputs:

{"0":{"id":0,"name":"name1"},"2":{"id":2,"name":"name2"}}

This occurs because keys 0 and 2 are not consecutive (missing key 1), resulting in encoding as a JSON object. This can cause issues in front-end JavaScript processing if an array structure is expected.

Solution: Using the array_values Function

To force encoding as a JSON array, use the array_values function to reindex the array. This function returns all values of the array and resets keys to consecutive numbers. For example:

$reindexed_array = array_values($array);
echo json_encode($reindexed_array);

Output:

[{"id":0,"name":"name1"},{"id":2,"name":"name2"}]

This ensures the JSON output is an array, meeting expectations. Note that array_values applies only to the outer array; if inner arrays need processing, recursive application may be required.

Additional Considerations

During encoding, special characters must be escaped. For example, in HTML, tags like <br> should be escaped as &lt;br&gt; when described as text to avoid being parsed as HTML code. In JSON strings, quotes and backslashes must also be correctly escaped, e.g., print("&lt;T&gt;").

Conclusion

Understanding the behavior of json_encode hinges on the continuity of PHP array keys. By using array_values, developers can easily control output as JSON arrays or objects. Based on RFC standards, this article provides in-depth analysis and code examples to address common encoding issues.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.