php输入数据提交排序 php选择排序代码( 二 )


-
input name="textfield4" type="text" id="textfield4" size="15" /
-
input name="textfield5" type="text" id="textfield5" size="15" /
input type="submit" name="button" id="button" value="https://www.04ip.com/post/提交" /
/form
?
if(isset($_POST['button'])){
echo "排序后的数据如下所示:";
echo 'br/';
$array=array(
"0"=$_POST['textfield'],
"1"=$_POST['textfield2'],
"2"=$_POST['textfield3'],
"3"=$_POST['textfield4'],
"4"=$_POST['textfield5'],);//把数字存到数组中
sort($array);//php函数排序从小到大
foreach($array as $key = $value){
echo $value;//循环输出值
echo 'br/';
}
}
?
php测试数组怎么排序?1、在test.php文件内,使用header设置test.php执行的编码为utf8,避免输出中文的时候出现乱码 。
2、在test.php文件内,创建一个测试的数组 , 例如,定义一个分类的数组,其对应的索引值分别为0 , 4,8 。
3、在test.php文件内,使用array_values()方法将上一步的数据重新排序,并且从0开始,把重新排序的数组保存在$result变量中 。
4、在test.php文件内,使用foreach方法遍历数组,其中$k为索引值,$v为索引值对应的数组值 。
5、在test.php文件内,使用echo方法输出数组中的索引值和对应的数组值即可 。
1使用PHP的排序函数对以下中的数组排序 。2.在表单上由用户输入学号 , 姓名和成绩这三列的数据 , 一共5行 。费了一段时间php输入数据提交排序,总算完成这个作业,也算是一种锻炼吧,以下是代码,虽然感觉效率比较低,为什么不用数据库呢php输入数据提交排序?
?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 "trtd学号/tdtd姓名/tdtd成绩td/tr";
for ($row = 1; $row6; $row++) {
echo "tr";
for ($col = 1; $col4; $col++) {
echo "tdinput type='text' name='stu_$row" . "_$col' size='10'/td";
}
echo "/tr";
}
echo "/table";
echo "input type='submit' name='bt' value='https://www.04ip.com/post/提交'";
echo "/form";
//转换数组
if (isset($_POST['bt'])) {
for ($row = 1; $row6; $row++)
for ($col = 1; $col4; $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'
?
PHP实现插入排序算法 插入排序(Insertion Sort) 是一种较稳定 简单直观的排序算法 插入排序的工作原理 是通过构建有序序列 对于未排序的数据 在有序序列中从后向前扫描 找到合适的位置并将其插入 插入排序 在最好情况下 时间复杂度为O(n);在最坏情况下 时间复杂度为O(n );平均时间复杂度为O(n )

推荐阅读