views:

237

answers:

6

When declaring an associative array, how do you handle the indentation of the elements of the array? I've seen a number of different styles (PHP syntax, since that's what I've been in lately). This is a pretty picky and trivial thing, so move along if you're interested in more serious pursuits.

1) Indent elements one more level:

$array = array(
    'Foo' => 'Bar',
    'Baz' => 'Qux'
    );

2) Indent elements two levels:

$array = array(
        'Foo' => 'Bar',
        'Baz' => 'Qux'
        );

3) Indent elements beyond the array constructor, with closing brace aligned with the start of the constructor:

$array = array(
            'Foo' => 'Bar',
            'Baz' => 'Qux'
        );

4) Indent elements beyond the array construct, with closing brace aligned with opening brace:

$array = array(
            'Foo' => 'Bar',
            'Baz' => 'Qux'
              );

Personally, I like #3—the broad indentation makes it clear that we're at a break point in the code (constructing the array), and having the closing brace floating a bit to the left of all of the array's data makes it clear that this declaration is done.

+2  A: 

Generally I will only use a multi-line format for an associative array if it's non-trivial in a coding sense, ie. something that is useful to reference while reading the code. Otherwise, (if it's a short array), I will put it on a single line.

If it's long or non-trivial, my IDE (Eclipse at the moment, until I can settle on a suitable alternative) handles most of the formatting for me. In general it turns out similar to your first example, with one level of indenting:

$array = array(
    'Foo' => 'Bar',
    'Baz' => 'Qux'
    );

I've grown to prefer it this way. One level of indenting is really all that's needed, and creates more horizontal space.

zombat
+1  A: 

I like:

$array = array('Foo' => 'Bar',
               'Baz' => 'Qux');
AndyMcKenna
+1  A: 

Besides the indentation, there also the issues of lining up the values since the keys will most likely have unequal lengths (at least some of them). So, for example, how do you deal with something like this:

'KeyOne' => 'Value1',
'KeyTwo' => 'Value2',
'KeyTwelve' => 'Value12',

In such cases I generally tab out the fat arrow at least 2 tabs to give something like this:

'KeyOne'       => 'Value1',
'KeyTwo'       => 'Value2',
'KeyTwelve'    => 'Value12',
ennuikiller
+6  A: 

Personally I always go:

$array = array(
  '1' => '2',
  3 => 4,
);

The indent is one tab level (typically 4 spaces, sometimes 2). I detest excessive white-space. This works well with nested arrays.

cletus
+3  A: 

I generally use this kind of indentation for array's declarations :

function test()
{
    $my_array = array(
        'a' => 1,
        'bcdef' => 2,
        'gh' => array(
            'glop',
            'test'
        ),
        'ijk' => 20,
    );
}

Quite similar to #1, but with this difference :

  • the final } is unindented

I never put many spaces arround the '=>' to align the values (like ennuikiller suggested) : I find that really hard to read, and often have my eyes jump to the wrong value ^^

Also note that I always put a ',' at the end of the last declaration :

  • it is perfectly valid
  • you don't have to add it when you add one more line to the array
  • when you add one line at the end of the array, you modifiy just one line : the one you are addin (and definitly not the one before, to add the ',' as it is already there) ; this helps with diffs and patches : less modified lines, easier to read

One more thing : this is what I do when I work on a project that doesn't specify formating rules ; else, I try to respect those as much as possible (so that formating is coherent between members of the project's team)

Pascal MARTIN
+1  A: 

I go with one that is similar to #4:

$array = array('Foo' => 'Bar',
               'Baz' => 'Qux'
               'Moo' => 'Cow');

I think this method allows for easier visability of items in the array, which is especially useful when the array is larger than 10 items.