unidox Posted August 3, 2010 Share Posted August 3, 2010 I have an array like this: $array[$row] = array ( 'name' = $name, 'discount' = $discount, 'price' = $price ); Now, I have about 200 of those in that array, and im trying to sort by the discount, highest 1st. How would I go about doing that? Thanks. Link to comment https://forums.phpfreaks.com/topic/209701-sorting/ Share on other sites More sharing options...
systemick Posted August 3, 2010 Share Posted August 3, 2010 Take a look at the usort function here: http://www.php.net/manual/en/function.usort.php You could define a function function compare ($array1, $array2) { if (array1['discount'] < array2['discount']) { return -1; } else if (array1['discount'] > array2['discount']) { return 1; } else { return 0; } } then pass the name of this function as the 2nd argument to usort Link to comment https://forums.phpfreaks.com/topic/209701-sorting/#findComment-1094735 Share on other sites More sharing options...
AbraCadaver Posted August 3, 2010 Share Posted August 3, 2010 Or array_multisort(). When the array is structured like yours it takes a little more work: foreach($array as $key => $val) { $discount[$key] = $val['discount']; } array_multisort($discount, SORT_DESC, $array); print_r($array); Link to comment https://forums.phpfreaks.com/topic/209701-sorting/#findComment-1094746 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.