array sorting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

array sorting

Post by s.dot »

original array:

Code: Select all

$array = array(B => 1, X => 3, A =>1, F => 3);
I run asort() on this and get the following result

Code: Select all

Array (
   [B] => 1
   [A] => 1
   [X] => 3
   [F] => 3
)
So that's cool. But now, when two or more keys have the same value, I'd like to sort those keys in alphabetical order, so the above array would look like this.

Code: Select all

Array (
   [A] => 1
   [B] => 1
   [F] => 3
   [X] => 3
)
I need to maintain the same order of the numbers, for example 1s first then 3s, but rearrange the keys alphabetically.

I hope I explained that well. =/
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ksort() and/or multisort() and/or usort()
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

yeahhh, but when I run any of those on the array they effect the whole thing. I need to sort by value (sort_numeric) , then sort the key alphabetically
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply