1使用PHP的排序函数对以下中的数组排序。 2.在表单上由用户输入学号,姓名和成绩这三列的数据,一共5行。

将用户输入的数据组合一个二维数组,根据成绩值对二维数组进行排序,并将结果以表格形式输出。
把答案发到我邮箱610085385@qq.com,谢谢了。

<?php
echo "请输入需要排序的数据:<br>";
echo "<form method=post>";
for($i=1;$i<6;$i++)
{

echo"<input type='text' name='stu[]' size='5'>";
if($i<5)
echo"-";
}
echo"<input type='submit' name='bt' value='提交'>";
echo"</form>";
if(isset($_POST['bt']))
{
$temp=0;
$stu=$_POST['stu'];
$num=count($stu);
echo"您输入的数据有: <br>";
foreach ($stu as $score)
{
echo $score."<br>";
}
for($i=0;$i<$num;$i++)
for($j=$i+1;$j<$num;$j++)
{
if($stu[$i]>$stu[$j])
{
$temp=$stu[$i];
$stu[$i]=$stu[$j];
$stu[$j]=$temp;
}
}
echo "排序后的数据如图下所示: <br>";
while(list($key,$value)=each($stu))
{
echo $value."<br>";
}
}
?>

费了一段时间,总算完成这个作业,也算是一种锻炼吧,以下是代码,虽然感觉效率比较低,为什么不用数据库呢?

<?php

header('Content-type:text/html;charset=utf-8');
//2维数组排序
function sysSortArray($ArrayData, $KeyName1, $SortOrder1 = "SORT_ASC", $SortType1 = "SORT_REGULAR")
{
if (!is_array($ArrayData)) {
return $ArrayData;
}

// Get args number.
$ArgCount = func_num_args();

// Get keys to sort by and put them to SortRule array.
for ($I = 1; $I < $ArgCount; $I++) {
$Arg = func_get_arg($I);
if (!eregi("SORT", $Arg)) {
$KeyNameList[] = $Arg;
$SortRule[] = '$' . $Arg;
} else {
$SortRule[] = $Arg;
}
}

// Get the values according to the keys and put them to array.
foreach ($ArrayData as $Key => $Info) {
foreach ($KeyNameList as $KeyName) {
${$KeyName}[$Key] = $Info[$KeyName];
}
}

// Create the eval string and eval it.
$EvalString = 'array_multisort(' . join(",", $SortRule) . ',$ArrayData);';
eval($EvalString);
return $ArrayData;
}

$keys = array('stu_no','name','price');

//输出表格
echo "请输入需要排序的数据:<br>";
echo "<form method=post>";
echo "<table>";
echo "<tr><td>学号</td><td>姓名</td><td>成绩<td></tr>";
for ($row = 1; $row < 6; $row++) {
echo "<tr>";
for ($col = 1; $col < 4; $col++) {
echo "<td><input type='text' name='stu_$row" . "_$col' size='10'></td>";
}
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' name='bt' value='提交'>";
echo "</form>";

//转换数组
if (isset($_POST['bt'])) {
for ($row = 1; $row < 6; $row++)
for ($col = 1; $col < 4; $col++) {
$key = $keys[$col-1];
$stu[$row][$key] = $_POST['stu_' . $row . '_' . $col];
}
echo '<pre>';
print_r($stu);
echo '</pre>';

}

//排序
$temp = sysSortArray($stu,'price',"SORT_ASC");

echo '<pre>';
print_r($temp);
echo '</pre>'

?>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-10-11
<?php
echo "请输入需要排序的数据:";
echo "<form method=post>"; //新建表单
for($i=1;$i<6;$i++) //循环生成文本框
{
//文本框的名字是数组名
echo "<input type=text name='stu[]' >";
}
echo "<input type=submit name=bt value='提交'>"; //提交按钮
echo "</form>";
if(isset($_POST['bt'])){
$sum=0;
$stu=$_POST['stu']; //取得所有文本框的值并赋予数组$stu
$num=count($stu); //计算数组$stu元素个数
echo "您输入的数据有:<br/>";
foreach($stu as $score){
echo $score."<br/>"; //输出接收的值
$sum=$sum+$score;
}
echo "排序后的数据为:<br/>";
function bubbleSort($arr)
{
// 外层循环控制需要比较的轮数
for ($i = 1, $len = count($arr); $i < $len; ++$i) {
for ($j = 0; $j < $len - $i; ++$j) { // 内层循环控制参与比较的元素
if ($arr[$j] > $arr[$j + 1]) { // 比较相邻的两个元素
$temp = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $temp;
}
}
}
return $arr;
}

$sort=bubbleSort($stu);
foreach ($sort as $sco){
echo $sco."<br>";
}

echo "5个数据和为:".$sum."<br/>";
echo "5个数据平均数为:".$sum/$num."<br/>";
}
?>

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网