Which is preferred, Associate arrays or Object? Which is preferred, Associate arrays or Object? json json

Which is preferred, Associate arrays or Object?


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.