如何在php中按升序排列给定字符串的单词


How to arrange the word of a given string in ascending oredr in php?

我想为我的编码得到一个适当的解决方案。

步骤1

。我首先把它分解成数组

步骤2

。然后拆分每个爆炸值

步骤3

。然后按for循环

排序
<?php 
$st ="It is a test";
$exp = explode(' ',strtolower($st));
print_r($exp);
$c =0;
$p =0;
for($i=0;$i<count($exp);$i++){
  $t = str_split($exp[$i]);
  $ch = $t[0];
   if($c==0){
       $arr[$p]=$exp[$i];      
       $temp = $ch;    
   }else{
       if(ord($ch)<ord($temp)){
          $tp = $arr[$p-1];
          $arr[$p-1] = $exp[$i];
          $arr[$p] = $tp;        
       }else{
          $arr[$p] = $exp[$i];
       }
} 
 $temp = $ch;  
 $p++;
 $c++; 
}
print_r($arr);
?>

不知道你想要什么,但我认为这是你想要的。

<?php 
$st ="It is a test";
$exp = explode(' ',strtolower($st));
sort($exp);
for($x = 0; $x <count($exp); $x++) {
     echo $exp[$x];
     echo "<br>";
}
?>