471,596 Members | 937 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,596 software developers and data experts.

Order array based on number of duplicates

I'm trying to figure out how to order an array based on the number of duplicates, and then remove all duplicates.

This is my code right now:

Expand|Select|Wrap|Line Numbers
  1. foreach($acID as $searchterms){
  2. $results = mysql_query(SELECT * FROM table
  3.      WHERE field LIKE '%$searchterms%')
  4.  
  5.    while($row = mysql_fetch_array($results)){
  6.          array_push($resultArray, $row['id']);
  7.          }
  8. }
My Array ends up looking like:
$resultArray[0] => 818
$resultArray[1] => 1009
$resultArray[2] => 818
$resultArray[3] => 300
$resultArray[4] => 818
$resultArray[5] => 300

What I want is a final array that will sort $resultArray so that the items with the most duplicates are listed first, and then removes duplicates. So, it should look like this:

$finalArray[0] => 818
$finalArray[1] => 300
$finalArray[2] => 1009

Any help would be greatly apreciated.
Jan 28 '09 #1
2 1936
Dormilich
8,656 Expert Mod 8TB
have a look at PHP's array functions (PHP: Array Functions). especially array_diff_* and array_intersect_* might be of interest.

there might be even some statement in the SQL syntax avoiding duplicate results, but you should ask for that in the MySQL forum.
Jan 28 '09 #2
Markus
6,050 Expert 4TB
I think DISTINCT can help you here. And also ORDER BY.
Jan 28 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by Sam Collett | last post: by
21 posts views Thread by Jaspreet | last post: by
3 posts views Thread by _eddie_ | last post: by
4 posts views Thread by Mokita | last post: by
2 posts views Thread by Mokita | last post: by
5 posts views Thread by fluk | last post: by
Thekid
3 posts views Thread by Thekid | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.