function aggComments( $spam_comments, $index_type, $remove_duplicates = 1, $hard_sort = 1, $display_counts = 1 ) { $return_array = array(); foreach( $spam_comments as $spam_comment ) // for each spam comment array_push( $return_array, trim( $spam_comment->$index_type ) ); // slice its data into new array if( $display_counts ) { $return_array_with_counts = array_count_values( $return_array ); $return_array = $ip_or_email = $count = array(); foreach( $return_array_with_counts as $key => $value ){ $ip_or_email[] = $key; $count[] = $value; } array_multisort( $count, SORT_DESC, $ip_or_email, SORT_ASC, $return_array_with_counts ); //STABLE SORT THAT MAINTAINS THE ORIGINAL ORDER FOR ELEMENTS THAT HAVE EQUAL COUNTS foreach( $return_array_with_counts as $key => $value ) if(!empty($key)) //SKIP ANY BLANK EMAIL ADDRESSES/URLS array_push( $return_array, $key . ' (' . $value . ')' ); return $return_array; } if( $remove_duplicates ) $return_array = array_flip( array_flip( array_reverse( $return_array, true ) ) ); // faster dedupe if( $hard_sort ) sort( $return_array ); // hard (rekey) sort return $return_array; }