Jump to content

[SOLVED]Sort array by popularity


ego0

Recommended Posts

I've hit a brick wall with the a script i'm making.

Say I have an array with the values ball,doll,cat,cat,dog,cat,ball,cat.

Is there a way to sort the array in order of the most common value inside the array.

I know that array_count_values can count the number of a certain value in an array.
So mayby I could run a loop to go through the array and count how many of each value are the array then put it into something with the value and the number of recurrences of the value then sort them that way?

Is there a much quicker way? Any help appreciated
Link to comment
https://forums.phpfreaks.com/topic/11870-solvedsort-array-by-popularity/
Share on other sites

Ok, It worked BUT the array now contains values like:

15,cat
2,dog
10,donkey

But when sorted using 'rsort' it is sorted by the first number rather than both number like '15'. The previous would sort like this:-

2,dog
15,cat
10,donkey

How can you sort an array numerically by both digits?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.