Reputation: 9778
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?
Upvotes: 4
Views: 185
Reputation: 8174
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.
Upvotes: 2