Give that a go, Works for me
<?php
$test = array();
$test[]="serverA";
$test[]="serverA.something.com";
$test[]="serverA";
$test[]="serverB";
$test[]="serverB.something.com";
$test[]="serverC";
$test[]="serverD.something.com";
sort($test);
$final = array();
$temp = "#";
for($i=0,$count = count($test);$i<$count;$i++){
if(substr( $test[$i], 0, strlen($temp) ) == $temp)
$temp = $test[$i];
else {
$final[] = $temp;
$temp = $test[$i];
}
}
//unset first
unset($final[0]);
//add in last
$final[] = $temp;
var_dump($final);
?>
gives me this
array
1 => string 'serverA.something.com' (length=21)
2 => string 'serverB.something.com' (length=21)
3 => string 'serverC' (length=7)
4 => string 'serverD.something.com' (length=21)
solved Merging several strings in PHP [closed]