1 function sortStr($str){ 2 3 $ary = str_split($str); 4 sort($ary); 5 $len = count($ary); 6 7 $arr = array(); 8 for($i=0; $i<$len; $i++){ 9 $n = ceil($i/2); 10 $k = ($i % 2 == 0) ? $len-1-$n : $n-1; 11 $arr[] = $ary[$k]; 12 } 13 14 return $arr; 15 } 16 17 $str = '952164387'; 18 print_r(sortStr($str));
// 输出结果
1 Array 2 ( 3 [0] => 9 4 [1] => 1 5 [2] => 8 6 [3] => 2 7 [4] => 7 8 [5] => 3 9 [6] => 6 10 [7] => 4 11 [8] => 5 12 )