title: Is there a PHP Array Group By function?
body: <pre><code>$arr1 = array('a' =&gt; '1', 'b' =&gt; 'blah', 'c' =&gt; 'whatever...',
                'aa' =&gt; '2', 'bb' =&gt; 'lbha', 'cc' =&gt; 'everwhat...', 'dd' =&gt; 'bingo',
                'aaa' =&gt; '3', 'bbb' =&gt; 'halb', 'ccc' =&gt; 'revetahw...');
  </code></pre>
  
  <p>In the array I have three different index lengths a,b and c are all 1 in length. aa,bb,cc and dd are all 2 in length. And aaa,bbb and ccc are all 3 in length.</p>
  
  <p>What I'm trying to do is find the index (group by length) with the most elements and the greatest length. </p>
  
  <p>so I would use aa,bb,cc,dd as they have 4 elements, this would return the index length of 2.</p>
  
  <p>I want to know how I can get the 2?</p>
  
  <p>Here is what I'm trying but it's not working</p>
  
  <pre><code>foreach($arr1 as $key =&gt; $data) {
     $index_length_arr[strlen($key)] = $index_length_arr[$key] + 1;
  }
  </code></pre>
  
  <p>Results:</p>
  
  <pre><code>Array
  (
      [1] =&gt; 1
      [2] =&gt; 1
      [3] =&gt; 1
  )
  </code></pre>
  
  <p>Expected Output:</p>
  
  <pre><code>Array
  (
      [1] =&gt; 3
      [2] =&gt; 4
      [3] =&gt; 3
  )
  </code></pre>
  
  <p>Then I could see that index (with the length of 2) had the most elements:</p>
  
  <pre><code>'aa' =&gt; '2', 'bb' =&gt; 'lbha', 'cc' =&gt; 'everwhat...', 'dd' =&gt; 'bingo',
  </code></pre>