iPixel Posted December 20, 2011 Share Posted December 20, 2011 Hey guys, quick question. I have an array that looks like (blow). How can i sort it based on array[0][3], the #'s 1,2,3,4,5,6 are what i want to sort by in ASC order. <?php Array ( [0] => Array ( [1] => Link Name [2] => URL [3] => 1 [4] => fy ) [1] => Array ( [1] => Link Name [2] => URL [3] => 2 [4] => fy ) [2] => Array ( [1] => Link Name [2] => URL [3] => 3 [4] => fy ) [3] => Array ( [1] => Link Name [2] => URL [3] => 4 [4] => fy ) [4] => Array ( [1] => Link Name [2] => URL [3] => 5 [4] => fy ) [5] => Array ( [1] => Link Name [2] => URL [3] => 6 [4] => fy ) ?> Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/253551-sorting-a-multidimensional-array/ Share on other sites More sharing options...
kicken Posted December 20, 2011 Share Posted December 20, 2011 Use usort() with a function that compares that index. Link to comment https://forums.phpfreaks.com/topic/253551-sorting-a-multidimensional-array/#findComment-1299760 Share on other sites More sharing options...
iPixel Posted December 20, 2011 Author Share Posted December 20, 2011 so something kind of like usort(array, function) and the function would look like... function () { if(array[0][3] < array[1][3]) { return something } } Link to comment https://forums.phpfreaks.com/topic/253551-sorting-a-multidimensional-array/#findComment-1299764 Share on other sites More sharing options...
iPixel Posted December 20, 2011 Author Share Posted December 20, 2011 I think i got it, seems to be working. <?php function cmp($a, $b) { if ($a[3] == $b[3]) { return 0; } return ($a[3] < $b[3]) ? -1 : 1; } usort($show, "cmp"); ?> Link to comment https://forums.phpfreaks.com/topic/253551-sorting-a-multidimensional-array/#findComment-1299766 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.