0

I have this array:

$arr = array('Stone', 'Gem', 'Star', ..., 'Star', 'Rock', 'Salt', ..., 'Metal', 'Cotton', 'Gem',...);
$array = array_count_values($arr);

So the output is like this:

Array
(
    [Stone] => 234
    [Gem] => 231
    [Star] => 123
    [Rock] => 232
)

Now I am trying to sort it alphabetically like,

[Gem] => ...
[Star] => ...
[Stone] => ...
[Rock] => ...

I tried this one:

sort($arr);
foreach($arr as $key => $value){
    echo $key.' : '.$value;
}

But the output is not what I have expected it look like this:

0 : 11 : 12 : 13 : 14 : 15 : 16 : 17 : 18 : 19 : 110 : 11 ...

Any ideas how I can correctly sort this?

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
gadss
  • 21,687
  • 41
  • 104
  • 154
  • possible duplicate of http://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php – Ricardo Silveira Sep 16 '15 at 04:17

2 Answers2

4

Have you tried ksort() ?

ksort($arr)

print($arr)

You can get the doc over here.

Om Prakash
  • 86
  • 1
  • 5
0

You are sorting by keys, not values, so you should use ksort() function.

Elon Than
  • 9,603
  • 4
  • 27
  • 37