Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Jordi Rivero

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Array group by count

Function for group by count of elements, with a optional parameter for sort on asc and desc mode

   1  
   2  <?php
   3  function ArrayGroupByCount($_array, $sort = false) {
   4     $count_array = array();
   5  
   6     foreach (array_unique($_array) as $value) {
   7         $count = 0;
   8  
   9  		foreach ($_array as $element) {
  10  		    if ($element == $value)
  11  		        $count++;
  12  		}
  13  	
  14  		$count_array[$value] = $count;
  15  	}
  16  	
  17  	if ( $sort == 'desc' )
  18  		arsort($count_array);
  19  	elseif ( $sort == 'asc' )
  20  		asort($count_array);
  21  
  22  	return $count_array;
  23  }
  24  ?>
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS