Sunday, February 1, 2015

PHP array sort ascending using closure function usort sorting

Any PHP developer who has used usort() or uasort() function before PHP support closure know it is a pain in the rear.  Having to declare a callback function just liters your code.  I have been doing this code which I found to be the simplest and most elegant:

<?php
usort($input_array, function(){
	if($a['something'] == $b['something']) return 0;
	return ($b < $a ? -1 : 1);
});
?>

No comments:

Post a Comment