Jump to content

Archived

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

ego0

[SOLVED]Sort array by popularity

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

Share this post


Link to post
Share on other sites
That's probably the best way to go about it. There are no sort functions that will give you what you want.

Share this post


Link to post
Share on other sites
Thanks, I'll try and go ahead with this method. *sends email to developers demanding a function to do this*

Share this post


Link to post
Share on other sites
Post something up when you have something and we can help you.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

×

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.