doddsey_65 Posted June 18, 2013 Share Posted June 18, 2013 Sorry for being in the wrong forum but I couldn't post in the Code Snippets section. Here is a quick function that will sort a multidimensional array by a given value function subval_sort(array $array, $subkey, $reverse = false) { if (empty($array)) { return array(); } $temp_array = array(); foreach ($array as $key => $value) { $temp_array[$key] = strtolower($value[$subkey]); } if ($reverse) { arsort($temp_array); } else { asort($temp_array); } $_array = array(); foreach ($temp_array as $key => $value) { $_array[] = $array[$key]; } return $_array; } It can be used as such: $array = [ 0 => [ 'value' => 6 ], 1 => [ 'value' => 2 ], 2 => [ 'value' => 1 ] ] $array = subval_sort($array, 'value'); This would return: [ 0 => [ 'value' => 1 ], 1 => [ 'value' => 2 ], 2 => [ 'value' => 6 ] ] And it can be reversed by specifying a third parameter (bool) $array = subval_sort($array, 'value', true); Quote Link to comment https://forums.phpfreaks.com/topic/279299-function-to-sort-multidimensional-array-by-a-value/ Share on other sites More sharing options...
Strider64 Posted June 18, 2013 Share Posted June 18, 2013 I'm confused, couldn't you just do the following? function sort_by_value($x, $y) { return ($x['value'] > $y['value']); } uasort($array, 'sort_by_value'); Quote Link to comment https://forums.phpfreaks.com/topic/279299-function-to-sort-multidimensional-array-by-a-value/#findComment-1436581 Share on other sites More sharing options...
Barand Posted June 18, 2013 Share Posted June 18, 2013 the function should be function sort_by_value($x, $y) { return ($x['value'] - $y['value']); } a sort function needs to return -, 0 or + values Quote Link to comment https://forums.phpfreaks.com/topic/279299-function-to-sort-multidimensional-array-by-a-value/#findComment-1436583 Share on other sites More sharing options...
Strider64 Posted June 18, 2013 Share Posted June 18, 2013 (edited) Your not returning anything, you're sorting a multidimensional array. The following works perfectly fine: <?php $students = array( 256 => array('name' => 'Jon', 'score' => 98.5), 2 => array('name' => 'Vance', 'score' => 85.1), 9 => array('name' => 'Stephen', 'score' => 94.0), 364 => array('name' => 'Steve', 'score' => 85.1), 68 => array('name' => 'Rob', 'score' => 74.6) ); function score_sort($x, $y) { return ($x['score'] < $y['score']); } // Print the array as is: echo '<h2>Array As Is</h2><pre>' . print_r($students, 1) . '</pre>'; // Sort by score: uasort($students, 'score_sort'); echo '<h2>Array Sorted By Score</h2><pre>' . print_r($students, 1) . '</pre>'; Edited June 18, 2013 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/279299-function-to-sort-multidimensional-array-by-a-value/#findComment-1436596 Share on other sites More sharing options...
kicken Posted June 18, 2013 Share Posted June 18, 2013 Your not returning anything, you're sorting a multidimensional array. Yes, your sort function does return something, hence the return keyword. What was being pointed out to you is that using < returns a true/false boolean, but what the function is supposed to return is a a number <0, =0, or >0. The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. The following works perfectly fine: Just by luck. The code is still incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/279299-function-to-sort-multidimensional-array-by-a-value/#findComment-1436619 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.