Hi i have a 2 dimensional array like this: a b c d 2423 a g b a 6234 h h s m 234 g h k p 7234 i want to sort it by the 5-th column (the numbers), so it should finally look like this: g h k p 7234 a g b a 6234 a b c d 2423 h h s m 234 what function should i use ? with what parameters ? would very appreciate if you gimme the code to do this Thanks
Very easy, using usort() function taking an example and modifing it a littyle: $arr - your array function cmp ($a, $b) { if ($a[4] == $b[4]) return 0; return ($a[4] > $b[4]) ? -1 : 1; } usort ($arr, "cmp"); check and say results
I already did like this sort($array, 4) it seems working .. you think it may cause problems in future ? Thanks
wrong here is sort description: void sort ( array array [, int sort_flags]) The optional second parameter sort_flags may be used to modify the sorting behavior using these values: SORT_REGULAR - compare items normally SORT_NUMERIC - compare items numerically SORT_STRING - compare items as strings
yes, you're right .. that won't work how i wrote it .. however, that was just an example .. in reality, in my program i have a situation with this kind of an array 2523, "descr1" 613, "descr2" 3246, "descr3" .... which needs to be sorted by the first column in this case, sort($arr, 0) works just perfect. It sorts the rows by the value of the first column. I donno, probably sort($arr) would do exactly the same (without that second argument), but i'll just leave it already .. it just works for me .. but in general, your solution is really nice .. thanks .. by reading the "usort" description on php site, i would never figure it out how it works ..