Count duplicate and Sort elements from results

Here is my code

$a = mysqli_query("SELECT results FROM t WHERE d >= (NOW() - INTERVAL 25 hour)");
$array_number = array();
while($coint = mysqli_fetch_assoc($a)){
 $array_number[] = $coint['results'];
foreach($array_number as $number) {
   foreach(explode("|", $number) as $num){
		echo $num;

The results:

420562324404111272846173467357739254246624763641733492186842113454662481335221227235710112743301733656768436521702223841584314306332481491819523770654757275120366385582443592914461733652232510 etc....

if i will change to like

echo "".$num.",";

the results will be

4,20,5,6,23,24,40,41,11,27,28,46,17,34,67,35,7,7,39,25,42,46,62,47,63,64,17,33,49,2,18,68,4,21,13,45,46,62,48,1,33,52,21,22,7,23,57,10,11,27,43,30,17,33,65,67,68,4,36,5,21,70,22,23,8,41,58,43,14,30,63,32,48,1,49,18,19,52,37,70,6,54,7,57,27,51,20,36,6,38,55,8,24,43,59,29,14,46,17,33,65,2,23,25,10, etc...

I need finally, first results by top 10 numbers (sorted by count of number), and second results by last 10 numbers (sorted by count of number).

top and last like this


1 Like

So first question: Does the order of the first set of results (4,20,5,6 etc) matter?

Second question: Is there a reason you’ve stored numbers concatenated with |'s?

Anyway, let’s try using some of PHP’s array functions to make this cleaner…

$a = mysqli_query("SELECT results FROM t WHERE d >= (NOW() - INTERVAL 25 hour)");
$array_number = array();
while($coint = mysqli_fetch_assoc($a)){
  $array_number = array_merge($array_number,explode("|",$coint['results'])); //Completes your array creation.
//If you REALLY need to echo out the results:
echo implode(",",$array_number);

//Now lets do some simplifying.
$array_number = array_count_values($array_number); //PHP has a function to do exactly what you want....
arsort($array_number); //Put them in order.
echo implode(",",array_slice(array_keys($array_number),0,10)); //Highest 10 values.
echo implode(",",array_slice(array_keys($array_number),-10)); //Lowest 10 values.

(This of course doesnt check to make sure there are at least 10 values. The pattern of matching frequencies is… not unpredictable, but doesn’t necessarily conform to ‘normal’ evaluation.)

  1. Yes, like 4,5,6,20
  2. No, but it is now, too late to change, too much build on this way. Now i need this with |'s

Work perfectly! Thanks you very very much!

One more question, if in future i will needed results like now with count of times. How to do this ?




5 = 55 times ,18 = 49 times ,3 = 47 times ,28 = 43 times,52 = 41 times,39 = 40 times,4 = 37 times,46 = 36 times,38 = 34 times,55 = 33 times

How to do this? thanks

$array_number is (at the end of that code) an array in the form (using your example)

5 => 55,
18 => 49,
3 => 47

AKA: The key is the number, and the value is the number of occurances of that number. (That’s what [FPHP]array_count_values[/FPHP] did)

Use a Foreach to walk through $array_number and pull the key and value.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.