PHP Cartesian Product of two associative arrays while combining keys and labels
<p>I have the following two array:</p>

<pre><code>&lt;?php
  $a = array( // actions
    'left' =&gt; 'LEFT',
    'middle' =&gt; 'MIDDLE',
    'right' =&gt; 'RIGHT',
  );
  $m = array( // modifiers
    'ctrl' =&gt; 'CTRL',
    'alt' =&gt; 'ALT',
    'shift' =&gt; 'SHIFT',
    'double' =&gt; 'DOUBLE',
  );
?&gt;
</code></pre>

<p>I need to get the cartesian product with a caveat of needing every combination of a combination as well.  It needs to scale if I add more options to the arrays of course.  So the generated list should output as follows:
</p>

<pre><code>&lt;?php
$options['left'] = 'LEFT';
$options['LEFT'] = array(
  'ctrl_left' =&gt; 'CTRL+LEFT',
  'alt_left' =&gt; 'ALT+LEFT',
  'shift_left' =&gt; 'SHIFT+LEFT',
  'double_left' =&gt; 'DOUBLE+LEFT',
  'ctrl_alt_left' =&gt; 'CTRL+ALT+LEFT',
  'ctrl_shift_left' =&gt; 'CTRL+SHIFT+LEFT',
  'ctrl_double_left' =&gt; 'CTRL+DOUBLE+LEFT',
  'alt_shift_left' =&gt; 'ALT+SHIFT+LEFT',
  'alt_double_left' =&gt; 'ALT+DOUBLE+LEFT',
  'ctrl_alt_shift_left' =&gt; 'CTRL+ALT+SHIFT+LEFT',
  'ctrl_alt_double_left' =&gt; 'CTRL+ALT+DOUBLE+LEFT',
  'ctrl_alt_shift_double_left' =&gt; 'CTRL+ALT+SHIFT+DOUBLE+LEFT',
);
$options['middle'] = 'MIDDLE';
$options['MIDDLE'] = array(
  'ctrl_middle' =&gt; 'CTRL+MIDDLE',
  'alt_middle' =&gt; 'ALT+MIDDLE',
  'shift_middle' =&gt; 'SHIFT+MIDDLE',
  'double_middle' =&gt; 'DOUBLE+MIDDLE',
  'ctrl_alt_middle' =&gt; 'CTRL+ALT+MIDDLE',
  'ctrl_shift_middle' =&gt; 'CTRL+SHIFT+MIDDLE',
  'ctrl_double_middle' =&gt; 'CTRL+DOUBLE+MIDDLE',
  'alt_shift_middle' =&gt; 'ALT+SHIFT+MIDDLE',
  'alt_double_middle' =&gt; 'ALT+DOUBLE+MIDDLE',
  'ctrl_alt_shift_middle' =&gt; 'CTRL+ALT+SHIFT+MIDDLE',
  'ctrl_alt_double_middle' =&gt; 'CTRL+ALT+DOUBLE+MIDDLE',
  'ctrl_alt_shift_double_middle' =&gt; 'CTRL+ALT+SHIFT+DOUBLE+MIDDLE',
);

$options['right'] = 'RIGHT';
$options['RIGHT'] = array(
  'ctrl_right' =&gt; 'CTRL+RIGHT',
  'alt_right' =&gt; 'ALT+RIGHT',
  'shift_right' =&gt; 'SHIFT+RIGHT',
  'double_right' =&gt; 'DOUBLE+RIGHT',
  'ctrl_alt_right' =&gt; 'CTRL+ALT+RIGHT',
  'ctrl_shift_right' =&gt; 'CTRL+SHIFT+RIGHT',
  'ctrl_double_right' =&gt; 'CTRL+DOUBLE+RIGHT',
  'alt_shift_right' =&gt; 'ALT+SHIFT+RIGHT',
  'alt_double_right' =&gt; 'ALT+DOUBLE+RIGHT',
  'ctrl_alt_shift_right' =&gt; 'CTRL+ALT+SHIFT+RIGHT',
  'ctrl_alt_double_right' =&gt; 'CTRL+ALT+DOUBLE+RIGHT',
  'ctrl_alt_shift_double_right' =&gt; 'CTRL+ALT+SHIFT+DOUBLE+RIGHT',
);
?&gt;
</code></pre>
