json_decode to array or to object - php

Latly I came upon a question where someone asked me, why I turn the output of json_decode into an assoc array.
For me it's easier to use assoc arrays than stdClasses and there are already many array_* functions which support data handling after I decode the json string.
After a short performance test it turns out, that the conversion to an assoc array is about 20% faster than the conversion to stdClass.
Nevertheless, the default behavior is $assoc = false. So I wonder, were are the benefits of using stdClasses when dealing with json data.
Are there any json types which can't be represented in assoc arrays, but in stdClasses?

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.

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.

Related

PHP: Array to object standardization good or bad?

I'm currently working on a project which requires me to only output objects when querying database and/or processing data. I have been hearing from other senior developers that, objects are "cheap" in term of memory and, I "kind of" agree with that.
So, my questions:
If I have an array as result of a query. Why not use the array "as is" since it already exists?
Is it really a good practice (besides standardization) to convert it into an object?
Does it really increase performance? (I can't grasp it because by converting the result into an object, I'm basically creating another entity, besides the already existing array, containing the same data).
In my opinion, I would say that you should NOT convert them into objects.
You already have them as arrays, so transforming/copying them into object would require more memory as at a given time, you end up with both, the objects and arrays. If you absolutely do not need any OOP functionality (inheritance, private proprieties/methods, ...) you should stick with your arrays.
But keep in mind: You can often fetch the database and get objects instead of arrays as result. (See PDO fetchAll) Then you already have your objects without having the array.
If you wonder how these both parties work in terms of performance, have a look at
Using arrays VS objects for storing data.
From what I understand, (but I might as well be wrong) PHP arrays are not arrays compared to the classical C arrays. A PHP array is a sort of object, since you have freedoms to i.e. change an array size (simply add a value). So you do not have the exact same performance as you would have with C arrays.
Conclusion: Feel free to keep your arrays. If you do not need the advantages of objects, no need to use ressources to convert them. After all, it's also a question of programming style, best practices and project guidelines.
It depends on what next is the code doing with the array.
If you leave the data in array you should be careful where you pass it next, because you do not want that array go all over the code base back and forth.
With array you cannot define any definable interface between classes or logic code blocks and that is not very predictable.
When you have array passed through 10 methods in 5 classes modifying the contents of that array its very hard to track what and where is happening to the contents.
I like this answer: https://www.reddit.com/r/PHP/comments/29eope/stop_abusing_arrays_in_php/cik8tet/

Best practice - REST API response decoded as array or object on client side

I have the following issue:
I make a call to a rest api. It returns a json with data. Now I need on client to process that data and do something with it (doesn't really matter what).
I can either decode the json as an object, or as an associative array. Are there any good practices saying that I should go one way or another (with solid reasons behind them)?
Personally I'm inclined towards object, but in some cases this may not be the best choice (for example when there is a numerical indexed array encoded in json, it will be decoded as array inside the object, so I'm ending up with two types of data - is not a big problem, but is a bit inconvenient).
JSON/JavascriptECMAScript clearly delineates between objects and arrays (as do many other languages, which call them lists and dictionaries, or hashes, or whatever). Objects are unordered key-value pairs, while arrays are ordered numerically indexed containers.
PHP doesn't make that distinction in its arrays, PHP's arrays are ordered key-value pairs also allowing numeric keys. But PHP also happens to have a concept of objects. json_decode simply offers you which way you want to map those ambiguous data types. It's entirely up to you.
There's not a lot you can do with stdClass objects, while arrays are very flexible data structures with tons of manipulation functions. Take your pick. It largely doesn't matter.

Which is preferred, Associate arrays or Object?

I'm doing a SOAP request in PHP. The result has an option to return either XML or JSON. I decided on JSON because I am familiar with json_decode. With json_decode, if the parameter 'true' is added, it returns an associative array without it the default is an Object.
This is for a train schedule. It's by station which includes trips and stops. How do I decided in my PHP application I'm writing if I should deal with this train station schedule data as an Object or associative array? What would be the deciding factor? What are the pros and cons to either?
It mostly comes down to which you're the most familiar with.
There are a couple considerations to keep in mind though:
PHP has a large set of functions for dealing with and manipulating arrays. These same functions will (for the most part) not work with StdClass objects. If you're going to need some of this functionality, arrays may be easier.
JSON differentiates between arrays (unkeyed lists of items), and objects (each item stored under a string "key"). If you care about the difference - you need to detect whether something was an array or an object in the original data - it can be difficult to do so with straight PHP arrays, and it may make more sense to go with objects.

Why does the PHP function "json_decode" return an object?

The PHP function json_decode (by default) returns an object. Switching the second argument will return an array.
Maybe I just don't understand objects, but I thought objects have properties and methods (maybe events too). Arrays only have properties.
Given that json_decode will only ever return properties and never methods, shouldn't it always return an array?
It returns an object because JSON defines an object structure. This is what the 'O' stands for in 'JSON'.
This is where the differences between languages starts to become more obvious.
Javascript uses objects where PHP might use an array with named keys. JS can't have named keys in an array, only in an object. Other languages have other limitations to how they structure their variables.
Using an object means that PHP is as consistent with other language implementations of JSON as possible. Since JSON is designed for cross-language communication, being consistent is important.
But as you say, in PHP it is sometimes easier to work with an array, so this is why PHP offers the option of converting it directly to an array.
But be aware that PHP arrays are not the same as JSON arrays and objects. PHP allows you to mix named and numbered array keys. This does not map well to JSON, so if you're using PHP arrays to work with JSON you have to be careful of it. If you're using PHP objects for your JSON work, then you won't have this mismatch in functionality.
Okay, so it seems you already knew this:
By default, json_decode will return a StdClass object. If you want an array, use:
json_decode($jsondata, true);
See: http://php.net/manual/en/function.json-decode.php
So, to answer "Why":
JSON is a format used to store hierachical datasets, much like how XML might have been used in the past. Because however Javascript is optimized for accessing object properties, no additional libraries need be present to work with JSON structures - they are actual objects in Javascript.
It is easier to parse JSON than XML, and relatively easy to translate into objects and/or arrays in back-end languages. In many languages outside of PHP, there is something called a Dictionary, or Hashtable, which is usually an object with key/value pairs.
PHP does not differentiate arrays and "associative arrays" other than contextually, so for a PHP developer it's natural to expect the result to be an associative array, and that option exists, but most likely for flexibility (and maybe because it decodes more naturally to the object) the object format exists.
I hope that explains. I also strongly recommend reading further on what JSON is (and is not) here:
http://json.org
json_decode returns by default an object from the stdClass class. This is the basic (top-level) generic class for objects. This class has no method nor attributes first.
But then you can add some "on the fly", what's called Dynamic Properties. More here:
Sometimes all that is necessary is a property bag to throw key value pairs into. One way is to use array, but this requires quoting all keys. Another way is to use dynamic properties on an instance of StdClass.
Hope it helps.
It returns the object of stdClass.
If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty. Arrays convert to an object with properties named by keys, and corresponding values. For any other value, a member variable named scalar will contain the value.

Why return object instead of array?

I do a lot of work in WordPress, and I've noticed that far more functions return objects than arrays. Database results are returned as objects unless you specifically ask for an array. Errors are returned as objects. Outside of WordPress, most APIs give you an object instead of an array.
My question is, why do they use objects instead of arrays? For the most part it doesn't matter too much, but in some cases I find objects harder to not only process but to wrap my head around. Is there a performance reason for using an object?
I'm a self-taught PHP programmer. I've got a liberal arts degree. So forgive me if I'm missing a fundamental aspect of computer science. ;)
These are the reasons why I prefer objects in general:
Objects not only contain data but also functionality.
Objects have (in most cases) a predefined structure. This is very useful for API design. Furthermore, you can set properties as public, protected, or private.
objects better fit object oriented development.
In most IDE's auto-completion only works for objects.
Here is something to read:
Object Vs. Array in PHP
PHP stdClass: Storing Data in an Object Instead of an Array
When should I use stdClass and when should I use an array in php5 oo code
PHP Objects vs Arrays
Mysql results in PHP - arrays or objects?
PHP objects vs arrays performance myth
A Set of Objects in PHP: Arrays vs. SplObjectStorage
Better Object-Oriented Arrays
This probably isn't something you are going to deeply understand until you have worked on a large software project for several years. Many fresh computer science majors will give you an answer with all the right words (encapsulation, functionality with data, and maintainability) but few will really understand why all that stuff is good to have.
Let's run through a few examples.
If arrays were returned, then either all of the values need to be computed up front or lots of little values need to be returned with which you can build the more complex values from.
Think about an API method that returns a list of WordPress posts. These posts all have authors, authors have names, e-mail address, maybe even profiles with their biographies.
If you are returning all of the posts in an array, you'll either have to limit yourself to returning an array of post IDs:
[233, 41, 204, 111]
or returning a massive array that looks something like:
[ title: 'somePost', body: 'blah blah', 'author': ['name': 'billy', 'email': 'bill#bill.com', 'profile': ['interests': ['interest1', 'interest2', ...], 'bio': 'info...']] ]
[id: '2', .....]]
The first case of returning a list of IDs isn't very helpful to you because then you need to make an API call for each ID in order to get some information about that post.
The second case will pull way more information than you need 90% of the time and be doing way more work (especially if any of those fields is very complicated to build).
An object on the other hand can provide you with access to all the information you need, but not have actually pulled that information yet. Determining the values of fields can be done lazily (that is, when the value is needed and not beforehand) when using an object.
Arrays expose more data and capabilities than intended
Go back to the example of the massive array being returned. Now someone may likely build an application that iterates over each value inside the post array and prints it. If the API is updated to add just one extra element to that post array then the application code is going to break since it will be printing some new field that it probably shouldn't. If the order of items in the post array returned by the API changes, that will break the application code as well. So returning an array creates all sorts of possible dependencies that an object would not create.
Functionality
An object can hold information inside of it that will allow it to provide useful functionality to you. A post object, for instance, could be smart enough to return the previous or next posts. An array couldn't ever do that for you.
Flexibility
All of the benefits of objects mentioned above help to create a more flexible system.
My question is, why do they use objects instead of arrays?
Probably two reasons:
WordPress is quite old
arrays are faster and take less memory in most cases
easier to serialize
Is there a performance reason for using an object?
No. But a lot of good other reasons, for example:
you may store logic in the objects (methods, closures, etc.)
you may force object structure using an interface
better autocompletion in IDE
you don't get notices for not undefined array keys
in the end, you may easily convert any object to array
OOP != AOP :)
(For example, in Ruby, everything is an object. PHP was procedural/scripting language previously.)
WordPress (and a fair amount of other PHP applications) use objects rather than arrays, for conceptual, rather than technical reasons.
An object (even if just an instance of stdClass) is a representation of one thing. In WordPress that might be a post, a comment, or a user. An array on the other hand is a collection of things. (For example, a list of posts.)
Historically, PHP hasn't had great object support so arrays became quite powerful early on. (For example, the ability to have arbitrary keys rather than just being zero-indexed.) With the object support available in PHP 5, developers now have a choice between using arrays or objects as key-value stores. Personally, I prefer the WordPress approach as I like the syntactic difference between 'entities' and 'collections' that objects and arrays provide.
My question is, why do they (Wordpress) use objects instead of arrays?
That's really a good question and not easy to answer. I can only assume that it's common in Wordpress to use stdClass objects because they're using a database class that by default returns records as a stdClass object. They got used to it (8 years and more) and that's it. I don't think there is much more thought behind the simple fact.
syntactic sugar for associative arrays
-- Zeev Suraski about the standard object since PHP 3
stdClass objects are not really better than arrays. They are pretty much the same. That's for some historical reasons of the language as well as stdClass objects are really limited and actually are only sort of value objects in a very basic sense.
stdClass objects store values for their members like an array does per entry. And that's it.
Only PHP freaks are able to create stdClass objects with private members. There is not much benefit - if any - doing so.
stdClass objects do not have any methods/functions. So no use of that in Wordpress.
Compared with array, there are far less helpful functions to deal with a list or semi-structured data.
However, if you're used to arrays, just cast:
$array = (array) $object;
And you can access the data previously being an object, as an array. Or you like it the other way round:
$object = (object) $array;
Which will only drop invalid member names, like numbers. So take a little care. But I think you get the big picture: There is not much difference as long as it is about arrays and objects of stdClass.
Related:
Converting to object PHP Manual
Reserved Classes PHP Manual
What is stdClass in PHP?
The code looks cooler that way
Objects pass by reference
Objects are more strong typed then arrays, hence lees pron to errors (or give you a meaningful error message when you try to use un-existing member)
All the IDEs today have auto-complete, so when working with defined objects, the IDE does a lot for you and speeds up things
Easilly encapsulate logic and data in the same box, where with arrays, you store the data in the array, and then use a set of different function to process it.
Inheritance, If you would have a similar array with almost but not similar functionality, you would have to duplicate more code then if you are to do it with objects
Probably some more reason I have thought about
Objects are much more powerful than arrays can be.
Each object as an instance of a class can have functions attached.
If you have data that need processing then you need a function that does the processing.
With an array you would have to call that function on that array and therefore associate the logic yourself to the data.
With an object this association is already done and you don't have to care about it any more.
Also you should consider the OO principle of information hiding. Not everything that comes back from or goes to the database should be directly accessible.
There are several reasons to return objects:
Writing $myObject->property requires fewer "overhead" characters than $myArray['element']
Object can return data and functionality; arrays can contain only data.
Enable chaining: $myobject->getData()->parseData()->toXML();
Easier coding: IDE autocompletion can provide method and property hints for object.
In terms of performance, arrays are often faster than objects. In addition to performance, there are several reasons to use arrays:
The the functionality provided by the array_*() family of functions can reduce the amount of coding necessary in some cases.
Operations such as count() and foreach() can be performed on arrays. Objects do not offer this (unless they implement Iterator or Countable).
It's usually not going to be because of performance reasons. Typically, objects cost more than arrays.
For a lot of APIs, it probably has to do with the objects providing other functionality besides being a storage mechanism. Otherwise, it's a matter of preference and there is really no benefit to returning an object vs an array.
An array is just an index of values. Whereas an object contains methods which can generate the result for you. Sure, sometimes you can access an objects values directly, but the "right way to do it" is to access an objects methods (a function operating on the values of that object).
$obj = new MyObject;
$obj->getName(); // this calls a method (function), so it can decide what to return based on conditions or other criteria
$array['name']; // this is just the string "name". there is no logic to it.
Sometimes you are accessing an objects variables directly, this is usually frowned upon, but it happens quite often still.
$obj->name; // accessing the string "name" ... not really different from an array in this case.
However, consider that the MyObject class doesn't have a variable called 'name', but instead has a first_name and last_name variable.
$obj->getName(); // this would return first_name and last_name joined.
$obj->name; // would fail...
$obj->first_name;
$obj->last_name; // would be accessing the variables of that object directly.
This is a very simple example, but you can see where this is going. A class provides a collection of variables and the functions which can operate on those variables all within a self-contained logical entity. An instance of that entity is called an object, and it introduces logic and dynamic results, which an array simply doesn't have.
Most of the time objects are just as fast, if not faster than arrays, in PHP there isn't a noticeable difference. the main reason is that objects are more powerful than arrays. Object orientated programming allows you to create objects and store not only data, but functionality in them, for example in PHP the MySQLi Class allows you to have a database object that you can manipulate using a host of inbuilt functions, rather than the procedural approach.
So the main reason is that OOP is an excellent paradigm. I wrote an article about why using OOP is a good idea, and explaining the concept, you can take a look here: http://tomsbigbox.com/an-introduction-to-oop/
As a minor plus you also type less to get data from an object - $test->data is better than $test['data'].
I'm unfamiliar with word press. A lot of answers here suggest that a strength of objects is there ability to contain functional code. When returning an object from a function/API call it shouldn't contain utility functions. Just properties.
The strength in returning objects is that whatever lies behind the API can change without breaking your code.
Example: You get an array of data with key/value pairs, key representing the DB column. If the DB column gets renamed your code will break.
Im running the next test in php 5.3.10 (windows) :
for ($i = 0; $i < 1000000; $i++) {
$x = array();
$x['a'] = 'a';
$x['b'] = 'b';
}
and
for ($i = 0; $i < 1000000; $i++) {
$x = new stdClass;
$x->a = 'a';
$x->b = 'b';
}
Copied from http://atomized.org/2009/02/really-damn-slow-a-look-at-php-objects/comment-page-1/#comment-186961
Calling the function for 10 concurrent users and 10 times (for to obtain an average) then
Arrays : 100%
Object : 214% – 216% (2 times slower).
AKA, Object it is still painful slow. OOP keeps the things tidy however it should be used carefully.
What Wordpress is applying?. Well, both solutions, is using objects, arrays and object & arrays, Class wpdb uses the later (and it is the heart of Wordpress).
It follows the boxing and unboxing principle of OOP. While languages such as Java and C# support this natively, PHP does not. However it can be accomplished, to some degree in PHP, just not eloquently as the language itself does not have constructs to support it. Having box types in PHP could help with chaining, keeping everything object oriented and allows for type hinting in method signatures. The downside is overhead and the fact that you now have extra checking to do using the “instanceof†construct. Having a type system is also a plus when using development tools that have intellisense or code assist like PDT. Rather than having to google/bing/yahoo for the method, it exists on the object, and you can use the tool to provide a drop down.
Although the points made about objects being more than just data are valid since they are usually data and behaviour there is at least one pattern mentioned in Martin Fowler's "Patterns of Enterprise Application Architecture" that applies to this type of cenario in which you're transfering data from one system (the application behind the API) and another (your application).
Its the Data Transfer Object - An object that carries data between processes in order to reduce the number of method calls.
So if the question is whether APIs should return a DTO or an array I would say that if the performance cost is negligible then you should choose the option that is more maintainable which I would argue is the DTO option... but of course you also have to consider the skills and culture of the team that is developing your system and the language or IDE support for each of the options.

Categories