Solved

Sort Associative PHP Array on value but maintain key order

Posted on 2008-10-28
6
459 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculate values in an array 18 35
myqsl update statement on phpMyAdmin 8 22
Update from TABLE-A to TABLE-B 5 39
Scope of $_SESSION 17 30
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now