Page 1 of 1

sorting multi dimntional array by certain index

Posted: Sun Nov 07, 2004 8:21 am
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

Posted: Sun Nov 07, 2004 9:08 am
by patrikG
[php_man]array_multisort[/php_man] check the user notes for good examples.

Posted: Mon Nov 08, 2004 7:38 am
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

Posted: Mon Nov 08, 2004 7:43 am
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);