In the past I’ve used emacs-php-align to align PHP arrays. However, there’s a more flexible alternative built into Emacs - align-regexp.

Here’s the original example I used.

$my_array = array(
    'key' => 'Value',
    'key_two' => 'Another value',
    'really_long_key' => 'Value',
    'k' => 'Value',
);

To align this with align-regexp, highlight the contents of the array and then:

  1. Run the align-regexp command (i.e. M-x align-regexp)
  2. Enter => at the “Align regexp” prompt

Which converts it into this:

$my_array = array(
    'key'             => 'Value',
    'key_two'         => 'Another value',
    'really_long_key' => 'Value',
    'k'               => 'Value',
);

As the name suggests, align-regexp takes a regular expression for alignment. This means it can also be used to align pretty much anything.

e.g.

{
    "key" : "Value",
    "key_two" : "Another value",
    "really_long_key" : 200,
    "k" : "Value",
}

With align-regexp using : becomes:

{
    "key"             : "Value",
    "key_two"         : "Another value",
    "really_long_key" : 200,
    "k"               : "Value",
}