473,698 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Order array based on number of duplicates

33 New Member
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 2122
Dormilich
8,658 Recognized Expert Moderator Expert
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 Recognized Expert Expert
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
3660
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. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
12
55564
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 also removed) So far I have (val is value, ar is array, returns new array):
21
13470
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 getting second highest value in an integer array. One method I know of is to make the 1st pass through the array and find the highest number. In the second pass we can find the highest number which is less than the number we obtained in the 1st pass.
3
8490
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
2679
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 solution. I just need to write a function that removes the duplicates in an array. heres my code for main #include <iostream> #include "accfuncs.cpp" using namespace std; void sort(int a, int h, int a2, int size); void printarray(int , int); //the...
4
13964
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 in an array (String). for (int k=0; k<myLength; k++){ boolean isthere=false; for (int l=0; l<sizeres; l++){ if (res.equals(my)){
2
1438
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 Set, but I realize that the order is really different from the first array. Does anyone know how can I have the order of the Set it was inserted first? The code I am using: // my is the array containing the information, which can can contain...
5
1553
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" , "activity1"); $arr2 = array("site", "description", "area1" , "activity2"); $arr3 = array("site", "description", "area2" , "activity2"); $arr4 = array("site1", "description1", "area3" , "activity2");
3
25073
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 compare a to b. If they're different then it will print out 'duplicates found'. The problem is that even after trying different arrays, some with duplicates some without, that 'b' rearranges the numbers. Here's an example: a='1934, 2311,...
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.