举例如下:'9235','42','9','5','8','32','136','343','45'
则拼接的最大的数为 : 9-9235-8-5-45-42-343-32-136
网上坑多,想了很久,
思路:
1、数组从小到大排序;
2、顺位比较,从第一位开始,大的在前(冒泡法);
关键是交换元素的条件,有点绕!
<?php $array = array('9235','42','9','5','8','32','136','343','45');
//$array = array('3','8','5','9'); sort($array); $max_lenth = strlen(max($array)); $end_key = sizeof($array); for($i=0;$i<$max_lenth;$i++){ for($k=0;$k<$end_key;$k++){ for($j=$k;$j<$end_key;$j++){ if(substr($array[$k],$i,1)<substr($array[$j],$i,1) AND substr($array[$k],$i,1)<>"" AND substr($array[$j],$i,1)<>"" AND substr($array[$k],0,$i+1)<substr($array[$j],0,$i+1)){
$temp = $array[$k];
$array[$k] = $array[$j];
$array[$j] = $temp;
}
}
}
} var_dump($array); foreach($array as $key =>$val){
echo $val;
}