473,221 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 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 2103
Dormilich
8,658 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

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

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
21
by: Jaspreet | last post by:
I was working on some database application and had this small task of getting the second highes marks in a class. I was able to do that using subqueries. Just thinking what is a good way of...
3
by: _eddie_ | last post by:
I'm building an array of strings on the fly from a database. What is the best method for eliminating duplicates? (I can do this before or after the strings are added to the array)
12
by: joestevens232 | last post by:
Hello, I'm having a little problem with my code...I have the program written but its just not doing what I want and needs a minor tweak and I've been trying to fix it for ohurs and can't find a...
4
by: Mokita | last post by:
Hello, I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates...
2
by: Mokita | last post by:
Hello, I am using Set class to remove duplicates from an array. The order in the array is important for my work. After removing the duplicates i create another array containing the elements of the...
5
by: fluk | last post by:
Hi Guys, I hope someone can help me with this, because i'm getting crazy to find a good way to do that! This is what I got by querying a db. $arr1 = array("site", "description", "area1" ,...
3
Thekid
by: Thekid | last post by:
I'm trying to figure out a way to find if there are duplicates in an array. My idea was to take the array as 'a' and make a second array as 'b' and remove the duplicates from 'b' using 'set' and then...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.