title: Generating Ordered (Weighted) Combinations of Arbitrary Length in PHP
body: <p>Given a list of common words, sorted in order of prevalence of use, is it possible to form word combinations of an arbitrary length (any desired number of words) in order of the 'most common' sequences. For example,if the most common words are 'a, b, c' then for combinations of length two, the following would be generated:</p>
  
  <pre><code>aa
  ab
  ba
  bb
  ac
  bc
  ca
  cb
  cc
  </code></pre>
  
  <p>Here is the correct list for length 3:</p>
  
  <pre><code>aaa
  aab
  aba
  abb
  baa
  bab
  bba
  bbb
  aac
  abc
  bac
  bbc
  aca
  acb
  bca
  bcb
  acc
  bcc
  caa
  cab
  cba
  cbb
  cac
  cbc
  cca
  ccb
  ccc
  </code></pre>
  
  <p>This is simple to implement for combinations of 2 or 3 words (set length) for any number of elements, but can this be done for arbitrary lengths? I want to implement this in PHP, but pseudocode or even a summary of the algorithm would be much appreciated!</p>