Sort Associative PHP Array on value but maintain key order

Hi,

Trying to sort an array

e.g.

array
(
 [apple] => 2
 [banana] => 1
 [goat]=>1
 [dog]=>2
)

sort on the numbers but keep key order

after sort

array
(
 [banana] => 1
 [goat]=>1
 [apple] => 2
 [dog]=>2
)

Thanks!!

BrentNJAsked:
Who is Participating?
 
ddrudikConnect With a Mentor Commented:
Seems there was a typo on that function, see this revision:
http://us.php.net/manual/en/function.asort.php#80802
0
 
MMDeveloperCommented:
try asort()
0
 
BrentNJAuthor Commented:
Tried that already. It didn't maintain the order of the fruit name but did do the numeric correctly
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
ddrudikCommented:
0
 
Ray PaseurCommented:
I think you may want this.  The man page for asort refers to the man page for sort for the sort flags.

HTH, ~Ray
<?php 
 
$sorted_array = asort($array, 'SORT_NUMERIC');

Open in new window

0
 
ddrudikCommented:
Thanks for the question and the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.