Solved

Sort Associative PHP Array on value but maintain key order

Posted on 2008-10-28
6
466 Views
Last Modified: 2012-05-05
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!!

0
Comment
Question by:BrentNJ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22825097
try asort()
0
 

Author Comment

by:BrentNJ
ID: 22825683
Tried that already. It didn't maintain the order of the fruit name but did do the numeric correctly
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22826258
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 27

Accepted Solution

by:
ddrudik earned 100 total points
ID: 22826274
Seems there was a typo on that function, see this revision:
http://us.php.net/manual/en/function.asort.php#80802
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 22830376
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
 
LVL 27

Expert Comment

by:ddrudik
ID: 23038771
Thanks for the question and the points.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question