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
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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.