php - How to sort 1 value in array to top? -
for example:
$array = ("1", "2", "3", "4", "5");
now want sort number 3 on top, result:
$array = ("3", "1", "2", "4", "5");
if want sort array except one, try :
function cmptotop($a, $b){ if($a === '3') return -1; //change comparison value if($b === '3') return 1; //change comparison value if($a === $b) return 0; return ($a < $b) ? -1 : 1; } $a = array('3', '2', '5', '6', '3', '1'); cmptotop($a, "cmp"); //result : array('3', '3', '1', '2', '5', '6')
Comments
Post a Comment