1.直接用rsort函数
<body>
<form action="" method="post">
<input type='text' name="txt1" />
<p>
<input type='text' name="txt2" />
<p>
<input type='text' name="txt3" />
<p>
<input type='text' name="txt4" />
<p>
<input type='text' name="txt5" />
<p>
<input type='submit' name='button' value='提交' />
</form>
<?php
if($_POST['button']=='提交'){
$a[0]=$_POST['txt1'];
$a[1]=$_POST['txt2'];
$a[2]=$_POST['txt3'];
$a[3]=$_POST['txt4'];
$a[4]=$_POST['txt5'];
print_r($a);
echo "<p>";
rsort($a);
foreach($a as $b){
echo $b." ";
}
}
?>
</body>
结果:
2.用冒泡排序
<body>
<form action="" method="post">
<input type='text' name="txt1" />
<p>
<input type='text' name="txt2" />
<p>
<input type='text' name="txt3" />
<p>
<input type='text' name="txt4" />
<p>
<input type='text' name="txt5" />
<p>
<input type='submit' name='button' value='提交' />
</form>
<?php
if($_POST['button']=='提交'){
$a1=$_POST['txt1'];
$a2=$_POST['txt2'];
$a3=$_POST['txt3'];
$a4=$_POST['txt4'];
$a5=$_POST['txt5'];
$a=array($a1,$a2,$a3,$a4,$a5);
echo "数字:";
print_r($a);
echo "<p>";
echo "<p>";
for($j=0;$j<4;$j++){
for($i=0;$i<4;$i++){//只需交换输入的个数-1即可
if($a[$i]<$a[$i+1]){
$temp=$a[$i];
$a[$i]=$a[$i+1];
$a[$i+1]=$temp;
}
}
}
echo "从大到小排序:";
print_r($a)."<br>";
}
?>
</body>
结果: