See Arrays in RFC 8259 The JavaScript Object Notation (JSON) Data Interchange Format:

An array structure is represented as square brackets surrounding zero or more values (or elements). Elements are separated by commas.

array = begin-array [ value *( value-separator value ) ] end-array

You are observing this behaviour because your array is not sequential - it has keys 0 and 2, but doesn't have 1 as a key.

Just having numeric indexes isn't enough. json_encode will only encode your PHP array as a JSON array if your PHP array is sequential - that is, if its keys are 0, 1, 2, 3, ...

You can reindex your array sequentially using the array_values function to get the behaviour you want. For example, the code below works successfully in your use case:

echo json_encode(array_values($input)).
Answer from user1836049 on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.json-encode.php
PHP: json_encode - Manual
... Like the reference JSON encoder, json_encode() will generate JSON that is a simple value (that is, neither an object nor an array) if given a string, int, float or bool as an input value.
๐ŸŒ
SitePoint
sitepoint.com โ€บ php
json_encode sometimes does or does not add keys for array elements - PHP - SitePoint Forums | Web Development & Design Community
If a PHP array does not have any ... a JSON array [indexed]. If on the other hand the PHP array has at least one defined key OR the keys are not sequential then it automatically becomes a JSON object......
Published: March 22, 2015
People also ask

How do I convert a PHP array to JSON?
Pass it to jsonencode(): jsonencode(['name' => 'Ada', 'age' => 36]) returns {"name":"Ada","age":36}. An array with keys 0, 1, 2... in order becomes a JSON array instead: json_encode(['a', 'b']) returns ["a","b"].
๐ŸŒ
coddy.tech
coddy.tech โ€บ docs โ€บ php โ€บ json_encode()
PHP json_encode(): Convert Arrays and Objects to JSON
How do I make json_encode output {} instead of [] for an empty array?
An empty PHP array always encodes as []. Use new stdClass() or (object) [] for an empty object, or the JSONFORCEOBJECT flag, which turns every array in the value into an object.
๐ŸŒ
coddy.tech
coddy.tech โ€บ docs โ€บ php โ€บ json_encode()
PHP json_encode(): Convert Arrays and Objects to JSON
Why does json_encode return false?
Usually because a string is not valid UTF-8, or the data contains NAN or INF. Call jsonlasterrormsg() to see the reason, or pass JSONTHROWONERROR so it throws a JsonException instead of returning false.
๐ŸŒ
coddy.tech
coddy.tech โ€บ docs โ€บ php โ€บ json_encode()
PHP json_encode(): Convert Arrays and Objects to JSON
๐ŸŒ
Coddy.Tech
coddy.tech โ€บ docs โ€บ php โ€บ json_encode()
PHP json_encode(): Convert Arrays and Objects to JSON
3 weeks ago - json_encode($value) returns the JSON text for a PHP value. An associative array becomes a JSON object, a list (keys 0, 1, 2...) becomes a JSON array, and strings, numbers, true, false and null become their JSON equivalents.
Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 22878219 โ€บ json-encode-turns-array-into-an-object
php - json_encode turns array into an object - Stack Overflow
April 5, 2014 - The zero index seems missing in your example, which could cause the problem, the reason may be your unset() call. A simple solution would be to call array_values() right before encoding.
๐ŸŒ
Carmatec
carmatec.com โ€บ home โ€บ understanding php json_encode() and json_decode()
Understanding PHP json_encode() and json_decode() - Carmatec
3 weeks ago - The json_encode() function in PHP converts a PHP variable (such as an array or object) into a JSON-formatted string. Conversely, json_decode() takes a JSON string and converts it back into a PHP variable, typically an array or object.
๐ŸŒ
Hamatti
notes.hamatti.org โ€บ technology โ€บ recipes โ€บ force-empty-array-into-object-in-json_encode-in-php
Force empty array into object in json_encode in PHP : Garden of Learning by Juhis
December 31, 2024 - An empty array has no keys so itโ€™s always a sequantial array in PHP and is encoded as [] for JSON. JSON_FORCE_OBJECT flag allows forcing arrays into objects but it turns everything into an object. [] becomes {} but also [1,2] becomes {0: 1, ...
Top answer
1 of 6
101
$arrayDecoded = json_decode($arrayEncoded, true);

gives you an array.

2 of 6
31

To answer the actual question:

Why does PHP turn the JSON Object into a class?

Take a closer look at the output of the encoded JSON, I've extended the example the OP is giving a little bit:

$array = array(
    'stuff' => 'things',
    'things' => array(
        'controller', 'playing card', 'newspaper', 'sand paper', 'monitor', 'tree'
    )
);
$arrayEncoded = json_encode($array);
echo $arrayEncoded;
//prints - {"stuff":"things","things":["controller","playing card","newspaper","sand paper","monitor","tree"]}

The JSON format was derived from the same standard as JavaScript (ECMAScript Programming Language Standard) and if you would look at the format it looks like JavaScript. It is a JSON object ({} = object) having a property "stuff" with value "things" and has a property "things" with it's value being an array of strings ([] = array).

JSON (as JavaScript) doesn't know associative arrays only indexed arrays. So when JSON encoding a PHP associative array, this will result in a JSON string containing this array as an "object".

Now we're decoding the JSON again using json_decode($arrayEncoded). The decode function doesn't know where this JSON string originated from (a PHP array) so it is decoding into an unknown object, which is stdClass in PHP. As you will see, the "things" array of strings WILL decode into an indexed PHP array.

Also see:

  • RFC 4627 - The application/json Media Type for JavaScript Object
  • RFC 7159 - The JavaScript Object Notation (JSON) Data Interchang
  • PHP Manual - Arrays

Thanks to https://www.randomlists.com/things for the 'things'

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ a โ€บ 25689620
php - json_encode changing array with one value to object - Stack Overflow
I think you should use is_array instead of is_object as arrays are returned when a json string is decoded to an associative array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-json_encode-function
PHP | json_encode() Function - GeeksforGeeks
June 17, 2019 - The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation.
๐ŸŒ
Elastic
elastic.co โ€บ docs โ€บ reference โ€บ elasticsearch โ€บ clients โ€บ php โ€บ configuration โ€บ dealing with json arrays and objects in php
Dealing with JSON arrays and objects in PHP | PHP
By using an explicit stdClass object, we can force the json_encode parser to correctly output an empty object, instead of an empty array. This verbose solution is the only way to acomplish the goal in PHPโ€ฆ