Sort PHP Arrays By Pair Value Length
Below is a simple user defined sort comparison callback function using uasort().
The Sort Callback Function
function sort_length_desc($a, $b) {
$alen = strlen($a);
$blen = strlen($b);
if ($alen < $blen)
return 1;
elseif ($alen == $blen)
return 0;
else
return -1;
}Testing The Sort Callback
$array = array(
'test',
'test two',
'test number three',
'test four',
);
uasort($array, 'sort_length_desc');Sorting Results
test number three
test four
test two
test
Bookmark/Search this post with:
Delicious
Digg
StumbleUpon
Reddit
Facebook