As per the documentation, you need to specify true as the second argument if you want an associative array instead of an object from json_decode. This would be the code:

$result = json_decode($jsondata, true);

If you want integer keys instead of whatever the property names are:

$result = array_values(json_decode($jsondata, true));

However, with your current decode you just access it as an object:

print_r($obj->Result);
Answer from Stephen on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ func_json_decode.asp
PHP json_decode() Function
The json_decode() function is used to decode or convert a JSON object to a PHP object. json_decode(string, assoc, depth, options) Store JSON data in a PHP variable, and then decode it into a PHP associative array: $jsonobj = '{"Peter":35,"B...
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.json-decode.php
PHP: json_decode - Manual
JSON can be decoded to PHP arrays by using the $associative = true option. Be wary that associative arrays in PHP can be a "list" or "object" when converted to/from JSON, depending on the keys (of absence of them).
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ php โ€บ 0uwqq41y โ€บ json-to-array-php-example
How do I convert JSON to PHP array?
To convert a JSON data string to a PHP array, you can use the json_decode($json) function. The json_decode() function accepts the JSON string as the first parameter and a few additional parameters to control the process of converting JSON to ...
๐ŸŒ
PHP: json_decode()
parthpatel.net โ€บ home โ€บ php โ€บ php: json_decode() | how to decode json to array in php
PHP: json_decode() | How to decode json to array in PHP | Parth Patel - a Web Developer
September 9, 2021 - When true is passed, JSON object will be converted into associative array; when false is passed, JSON object will be returned as stdClass object; when NULL is passed, it will return associative array or object depending on the JSON_OBJECT_AS_ARRAY ...
๐ŸŒ
W3Docs
w3docs.com โ€บ php
How to JSON Decode a String into an Array with PHP
But, note that since the assoc parameter is set to FALSE, it is necessary to set it to TRUE for retrieving an array. ... <?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json)); var_dump(json_decode($json, true)); ?>
๐ŸŒ
PHPpot
phppot.com โ€บ php โ€บ php-object-to-array
PHP Object to Array Convert using JSON Decode - PHPpot
This quick example performs a PHP object to array conversion in a single step. It creates an object bundle and sets the properties. It uses JSON encode() decode() function for the conversion.
๐ŸŒ
Edureka Community
edureka.co โ€บ home โ€บ community โ€บ categories โ€บ web development โ€บ php โ€บ json decode to array
json decode to array | Edureka Community
June 2, 2022 - I want to decode a JSON string into an array but I am getting this error. Fatal error: Cannot use ... Result']); ?> Can someone help me fix this?
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_json.asp
PHP and JSON
The json_decode() function returns an object by default. The function has a second parameter, and when set to true, JSON objects are decoded into associative arrays.
๐ŸŒ
Jonathan Suh
jonsuh.com โ€บ blog โ€บ convert-loop-through-json-php-javascript-arrays-objects
Convert and Loop through JSON with PHP and JavaScript Arrays/Objects โ€” Jonathan Suh
<?php // JSON string $someJSON = '[{"name":"Jonathan Suh","gender":"male"},{"name":"William Philbin","gender":"male"},{"name":"Allison McKinnery","gender":"female"}]'; // Convert JSON string to Array $someArray = json_decode($someJSON, true); print_r($someArray); // Dump all data of the Array echo $someArray[0]["name"]; // Access Array data // Convert JSON string to Object $someObject = json_decode($someJSON); print_r($someObject); // Dump all data of the Object echo $someObject[0]->name; // Access Object data ?>
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ php-tutorial โ€บ php-json-parsing.php
How to Encode and Decode JSON Data in PHP - Tutorial Republic
By default the json_decode() function returns an object. However, you can optionally specify a second parameter $assoc which accepts a boolean value that when set as true JSON objects are decoded into associative arrays. It is false by default.
๐ŸŒ
PHPpot
phppot.com โ€บ php โ€บ json-to-array-php
Convert JSON to Array in PHP with Online Demo - PHPpot
Creating an online converter for JSON to an array decoding with a single line using PHP json_decode native function.
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ php โ€บ zg4d50d8 โ€บ php-json-decode-example
How do I decode JSON in PHP?
To decode a JSON string or file in PHP, you can use the json_decode($json, $assoc, $depth, $options) function. The first parameter specifies the string to be decoded. The second optional parameter sets whether the returned object should be converted ...
๐ŸŒ
W3Resource
w3resource.com โ€บ php-exercises โ€บ file โ€บ php-file-handling-exercise-17.php
PHP JSON file: Read and convert to an associative array
We then use json_decode($jsonContents, true) to decode the JSON string into a PHP associative array. The second argument true specifies that we want an associative array rather than an object.
๐ŸŒ
Reintech
reintech.io โ€บ blog โ€บ php-json-encoding-decoding-web-services
PHP and JSON: Encoding, Decoding, and Web Services | Reintech media
April 28, 2023 - The json_decode() function parses JSON strings into PHP data structures. The second parameter determines whether to return an associative array (true) or a stdClass object (false, default).
Top answer
1 of 2
7

This might get closed as opinion based, but for me, I would typically decode to whatever data structure makes the most sense for the use case.

For example, say the JSON described a single item like a book and looked something like this:

{
    "title": "Cool Book",
    "author": "Amazing Author",
    "publisher": "Evil Corporation",
    ...
}

To me that is an object, in that it is a single item with different properties. I would be likely to want to treat it like an object in my subsequent code, so I would decode it as an object.

Now if the JSON contained the sort of data that may represent a dictionary, map, hash table, etc. sort of structure, where all the key-value pairs were, in essence, similar items, just with different lookups and mapped values, I might consider decoding to an associative array. Maybe a good example of that would be a country code to country name map like this:

{
    "AF": "Afghanistan",
    "AX": "Aland Islands",
    "AL": "Albania",
    "DZ": "Algeria",
    ...
}

I might be inclined to decode this to an associative array, because I don't need any object-oriented representation of this information, since I am just using this for key-value lookups.

To answer your question about other data structures that can be represented in JSON, officially there are only two data structures supported in JSON - objects and numerically-indexed arrays. This is because of the javascript-based roots of the serialization format, where, for example, the concept of an "out-of-the-box" associative array doesn't exist.

You will however find that a number of JSON encoding/decoding libraries across languages do add support for other data structures or types, typically adding handling behavior around primitive data types, but I would not rely on this unless you fully understand the data structures that are going to be passed and how they are going to be encoded/decoded across all applications that might pass the data around.

For example, PHP provides support for certain primitives as shown in this note from the json_encode() documentation:

Note: 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, integer, float or boolean as an input value. While most decoders will accept these values as valid JSON, some may not, as the specification is ambiguous on this point. To summarise, always test that your JSON decoder can handle the output you generate from json_encode().

Finally, with regards to performance, if you get to the point in your application development where the number one concern is optimizing performance for execution time, memory utilization, etc. and you have reason to believe that relatively substantial gains can be made by optimizing the JSON deserialization (and subsequent data access) logic, then you should ultimately test your application with representative data and see what works best for you. My guess is that this would be along the lines of a micro-optimization for most applications.

2 of 2
0

I think the reason is, that JSON is "JavaScript Object Notation" and therefore people expect an object. If you look at json.org the an object is defined as an unordered set of name/value pairs and an array is for ordered collections of values, like people are used to it in javascript. This can also be found in the RFC 4627:

The terms "object" and "array" come from the conventions of JavaScript.

๐ŸŒ
PHP
durak.org โ€บ sean โ€บ pubs โ€บ software โ€บ php-8.3.0 โ€บ function.json-decode.html
Decodes a JSON string - PHP 8.3.0 Manual / Documentation
var_dump(json_decode($json, true, 4)); echo 'Last error: ', json_last_error_msg(), PHP_EOL, PHP_EOL; var_dump(json_decode($json, true, 3)); echo 'Last error: ', json_last_error_msg(), PHP_EOL, PHP_EOL; ?> ... array(1) { [1]=> array(2) { ["English"]=> array(2) { [0]=> string(3) "One" [1]=> string(7) "January" } ["French"]=> array(2) { [0]=> string(3) "Une" [1]=> string(7) "Janvier" } } } Last error: No error NULL Last error: Maximum stack depth exceeded
๐ŸŒ
Jobtensor
jobtensor.com โ€บ Tutorial โ€บ PHP โ€บ en โ€บ JSON
PHP JSON - json_encode(), json_decode() | jobtensor
The json_encode() function is used to encode an array into JSON format. <?php $countries = array("Mark" => "USA", "Raymond" => "UK", "Jeff" => "JPN", "Mike" => "DE"); echo json_encode($countries); The json_decode() function is used to decode a JSON object into a PHP associative array or object.