This is probably not something you will deeply understand until you work on a major software project for several years. Many new specialists in the field of computer science will give you an answer with all the right words (encapsulation, functionality with data and maintainability), but few will understand why all this is good.
Let's skip a few examples.
- If the arrays were returned, then either all values must be calculated in front, or many small values need to be returned, from which you can build more complex values.
Think of an API method that returns a list of WordPress posts. These posts have authors, authors have names, an email address, perhaps even profiles with their biographies.
If you return all messages in the array, you will either have to limit yourself to returning an array of mail identifiers:
[233, 41, 204, 111]
or returns a massive array that looks something like this:
[ 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 the list of identifiers is not very useful for you, because then you need to make an API call for each identifier in order to get some information about this message.
The second case will provide more information than you need 90% of the time, and will do more work (especially if any of these fields is very difficult to build).
An object, on the other hand, can provide you with access to all the information you need, but so far it has not actually pulled this information. The determination of field values can be done lazily (i.e. when the value is necessary, and not in advance) when using the object.
- Arrays provide more data and capabilities than anticipated.
Let's go back to the example of a massive array that is returned. Now someone can probably create an application that iterates over each value inside the post array and prints it. If the API is updated to add only one additional element to this post-array, then the application code is about to break, as it will print a new field, which probably shouldn't. If the order of the elements in the message array returned by the API changes, this will also violate the application code. Therefore, returning an array creates all kinds of dependencies that the object would not create.
An object can store information inside it, which allows it to provide you with useful functions. For example, the post object may be smart enough to return previous or next posts. The array could not do this for you.
All the advantages of the above objects help create a more flexible system.