How to organize arrays

Asked

Viewed 79 times

3

In the documentation of Pear, it sets a pattern for how to mount the structure of an array:

$some_array = array(
    'foo'  => 'bar',
    'spam' => 'ham',
);

This way, which I already use, separates in a legible way the elements, but what about a array with say, 100 elements. The code will be very extensive, even so is correct?

Is this the only pattern to follow? There are other indications of how this structure should be organized?

  • With 100 elements it would be good to store it in the database.

  • It was just an assumption, @rray. But let’s say it’s an array containing settings to be saved or manipulated.

1 answer

3


No, it’s the same thing. There’s no more organized than this unless there’s some specific situation that helps. You can skip a line to separate blocks that make sense, or you can reevaluate if you need all elements together in array, depends on the situation.

Anyway, it’s taste. This is a good pattern but the important thing is to be readable, worse would be to squeeze everything because it has too much information.

If it makes sense to put it in the code, put it in the code. Only leave it out of the code if there’s a reason for it. If the information should come from an external source because the requirement requires this, be it to facilitate maintenance, configuration, etc. then it doesn’t matter why the data will be loaded into array without having to worry about the syntax getting big and confusing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.