Sorting an array by a keys value in the array

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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Sorting an array by a keys value in the array

Post by impulse() »

I have an array that has a layout:

$array[0][0] = "Something";
$array[0][1] = 20;
$array[1][0] = "Something else";
$array[1][1] = 50;

Is there an existing funtion to sort descending by the numeric value?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Can you show us the expected result? I'm not sure I understand you.
There are 10 types of people in this world, those who understand binary and those who don't
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

No problem:

I'd want key 1 of the array to be top of the list because 50 is the higher value and array key 0 to be bottom of the list because it's the lower number. I'll add some more values so it's more understandable.

Before sort:

Code: Select all

$array[0][0] = "Something";
$array[0][1] = 20;
$array[1][0] = "Something else";
$array[1][1] = 50; 
$array[2][0] = "Something else again";
$array[2][1] = 13;
$array[3][0] = "Last time";
$array[3][1] = 400;
After sort:

Code: Select all

$array[3][0] = "Last time";
$array[3][1] = 400;
$array[1][0] = "Something else";
$array[1][1] = 50; 
$array[0][0] = "Something";
$array[0][1] = 20;
$array[2][0] = "Something else again";
$array[2][1] = 13;
If there are still problems understanding then let me know.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Use uasort :)
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply