Jump to content

php array sorting help needed


Recommended Posts



I have a small problem. I have an array something like the following:


array[0] = "a"

array[1] = "b"

array[2] = "c"

array[3] = "a"

array[4] = "a"


Now I want to sort this array with respect to the frequency of occurrence of each character. So, basically this array should be rearranged as,


array[0] = "a"

array[1] = "a"

array[2] = "a"

array[3] = "b"

array[4] = "c"


How would this be done?

Link to comment
Share on other sites

i have never done this before but maybe array_count_values() could do it something like:

$array[0] = "a";
$array[1] = "b";
$array[2] = "c";
$array[3] = "b";
$array[4] = "b";
$temp = array_count_values($array);
$array = array();
foreach ($array as $value => $count) {
for ($i = 0; $i < $count; $i++) {
	$array[] = $value;			



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.

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.