Nov 18

I had a project I was working on yesterday and I needed to sort out an array in order of most to least repeated, so I came up with this:

$array = array_count_values($array);
array_multisort($array, SORT_DESC);

If you don't want the frequency that they occur in as the value, you can do this:

$array = array_count_values($array);
array_multisort($array, SORT_DESC);
$array = array_keys($array);

Hope this helps someone. :)

  • Digg
  • StumbleUpon
  • del.icio.us
  • Reddit

2 Responses

  1. Indy Says:

    Usually if you have many items and use a database, it's faster to use SQL's ORDER BY function.

  2. admin Says:

    Yes, this is generally only good for things like regular expressions or similar items.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.