heffe6 Posted January 13, 2010 Share Posted January 13, 2010 Hello everyone, Im trying to sort this array: Array ( [35] => Array ( [first_name] => Dave [last_name] => Park [cost] => 35 [amt] => 3 ) [46] => Array ( [first_name] => Steven [last_name] => Lewis [cost] => 60 [amt] => 6 ) ... etc I'd like to sort the first level array by the value in [last_name], then by the value [first_name], and maintain all keys. Can anyone help? Link to comment https://forums.phpfreaks.com/topic/188301-sort-problem/ Share on other sites More sharing options...
dgoosens Posted January 13, 2010 Share Posted January 13, 2010 you could use uasort() and write your own comparison filter... http://uk.php.net/manual/en/function.uasort.php Link to comment https://forums.phpfreaks.com/topic/188301-sort-problem/#findComment-994071 Share on other sites More sharing options...
heffe6 Posted January 13, 2010 Author Share Posted January 13, 2010 Thanks for your help, this code seems to work: function sort($a, $b) { if ($a['list'] == $b['list']) { return 0; } return ($a['list'] < $b['list']) ? -1 : 1; } Then i call uasort($d, 'sort'); When I output my array, the sort works. But I get the following warnings: Warning (2): sort() expects parameter 2 to be long, array given [APP\models\timesheet.php, line 103] Warning (2): uasort() [function.uasort]: Array was modified by the user comparison function [APP\models\timesheet.php, line 103] Any ideas? Link to comment https://forums.phpfreaks.com/topic/188301-sort-problem/#findComment-994082 Share on other sites More sharing options...
PravinS Posted January 13, 2010 Share Posted January 13, 2010 Try this function function sort_array($array, $key, $order) { if ($order=="DESC") $or="arsort"; else $or="asort"; for ($i = 0; $i < sizeof($array); $i++) { $sort_values[$i] = $array[$i][$key]; } $or($sort_values); reset ($sort_values); while (list ($arr_key, $arr_val) = each ($sort_values)) { $sorted_arr[] = $array[$arr_key]; } return $sorted_arr; } Link to comment https://forums.phpfreaks.com/topic/188301-sort-problem/#findComment-994083 Share on other sites More sharing options...
dgoosens Posted January 13, 2010 Share Posted January 13, 2010 Thanks for your help, this code seems to work: function sort($a, $b) { if ($a['list'] == $b['list']) { return 0; } return ($a['list'] < $b['list']) ? -1 : 1; } Then i call uasort($d, 'sort'); When I output my array, the sort works. But I get the following warnings: Warning (2): sort() expects parameter 2 to be long, array given [APP\models\timesheet.php, line 103] Warning (2): uasort() [function.uasort]: Array was modified by the user comparison function [APP\models\timesheet.php, line 103] Any ideas? try using another name for your function than "sort" sort() also is a PHP fnc maybe that causes the error... apart from that, your code seems fine to me Link to comment https://forums.phpfreaks.com/topic/188301-sort-problem/#findComment-994091 Share on other sites More sharing options...
heffe6 Posted January 13, 2010 Author Share Posted January 13, 2010 Thanks for your help everyone. My problem was I was calling this from within a class, (I failed to mention that) and I was having trouble accessing my custom sorting function. Another look through the manual, and I figured I had to do this: uasort($arr, array("className", "functionName")); Link to comment https://forums.phpfreaks.com/topic/188301-sort-problem/#findComment-994306 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.