title: Combining keys by similar values in PHP
body: <p>I am trying to combine keys when they have similar values, and expect the result as a string. Actually I had no idea how to achieve this, although I run a few tests.</p>
  
  <pre><code>$array = array(
    'a' =&gt; 65,
    'b' =&gt; 31,
    'c' =&gt; 100,
    'd' =&gt; 31,
    'e' =&gt; 31,
    'f' =&gt; 31,
    'h' =&gt; 23,
    'i' =&gt; 23,
    'j' =&gt; 23,
    'k' =&gt; 23,
    'l' =&gt; 48,
    'm' =&gt; 48,
  );
  
  $results = array();
  foreach ($array as $k =&gt; $v) {
    // This is my attempt among others to no luck.
    // Can not use array_key_exists because values are unpredictable.
    $similars[$v] = $k;
    $results[$v] = implode(", ", array_unique($similars)) . ' : ' . $v;
  }
  
  var_dump(implode("\n ", $results));
  </code></pre>
  
  <p>You can view the output:
  <a href="http://codepad.org/ECekF3dq" rel="nofollow">http://codepad.org/ECekF3dq</a></p>
  
  <p>I am almost there, but obviously wrong :(</p>
  
  <p><strong>Not expected:</strong></p>
  
  <pre><code>string(72) "a : 65
   a, f, c : 31
   a, b, c : 100
   a, f, c, k : 23
   a, f, c, k, m : 48"
  </code></pre>
  
  <p><strong>Expected:</strong></p>
  
  <pre><code>a : 65
  b, d, e, f : 31
  c : 100
  h, i, j, k : 23
  l, m : 48
  </code></pre>
  
  <p>Those with the same values should collapse as one line.</p>
  
  <p>Thanks for any hint.</p>