sorting multi dimntional array by certain index

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
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

sorting multi dimntional array by certain index

Post by pelegk2 »

i have an array like this :
(
(1,33,"xcfd",45,"sdfsdf"),
(321,314,"xcfd",1,"yre")
}

and i want to sort this array's by the 4th key of each array
so in the first array the value is : 45 amd i nthe second array is 1 !!!!
and the result should give the second array as first and the first array as second
how do i do that?thnaks i nadvance
peleg
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

[php_man]array_multisort[/php_man] check the user notes for good examples.
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

Post by pelegk2 »

hi patrikG i did so
and i tried to use there code to sort by an index that is a number and not a string and it didnt work
any idea?
thnaks in advance
peleg
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$a = array(
  array(1,   33,   "xcfd", 45, "sdfsdf"),
  array(321, 314, " xcfd", 1,   "yre"),
);
usort($a, create_function('$a,$b', 'if($a[3]==$b[3]) return true; return $a[3]<$b[3] ? -1 : 1;'));
var_dump($a);
Post Reply