Which is preferable to associate arrays or an object?

I am making a SOAP request in PHP. As a result, it is possible to return XML or JSON. I decided to use 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 Object.

This is for train schedules. This is a station that includes trips and stops. How did I decide in my PHP application that I am writing if I should consider the schedule data at the station as an object or an associative array? What will be the decisive factor? What are the pros and cons?

+4
source share
1 answer

It basically comes down to what you are most familiar with.

There are several considerations to keep in mind:

  • PHP has a large set of functions for working with arrays and managing them. These same functions (for the most part) will not work with StdClass objects. If you need some of these functions, arrays can be simpler.

  • JSON distinguishes between arrays (unblocked lists of elements) and objects (each element is stored under the "key" line). If you care about the difference — you need to determine if something was an array or an object in the source data — it can be difficult to do with direct PHP arrays , and it might make sense to go with objects.

+2
source

Source: https://habr.com/ru/post/1492009/


All Articles