How do I sort an array by string length then by value in PHP?
Incorporate both checks into your comparator:
function lensort($a,$b){
$la = strlen( $a); $lb = strlen( $b);
if( $la == $lb) {
return strcmp( $a, $b);
}
return $la - $lb;
}
You can see from this demo that this prints:
Array
(
[0] => A
[1] => B
[2] => C
[3] => AA
[4] => BB
[5] => BC
)