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. Quote Link to comment 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 Quote Link to comment 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); Quote Link to comment 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.