对数组进行排序而不替换php

时间:2019-02-21 11:33:17

标签: php arrays

如何在不替换索引元素的情况下对数组进行排序? 当我对一个不存在的数组进行降序排序时,索引值将被替换,是否有任何办法对数组进行降序排序并保留原始索引?

// Get Current Session
$Res_Sess = mysql_query("SELECT sessionid from tbl_session WHERE status=1 ORDER BY sessionid desc limit 1");
$Row_Sess = mysql_fetch_array($Res_Sess);
$Session = $Row_Sess[0];

// Get Batch
$Res_Bat = mysql_query("SELECT batchid,batchname,code FROM tbl_batch where status=1 and batchid!=12 ORDER BY batchid asc");
$allAbsent = array();
$indexes = 0;

while ($Row_Bat = mysql_fetch_array($Res_Bat)) {
    $Batch = $Row_Bat['batchid'];
    $Bat_Code = $Row_Bat['code'];
    $qatt1 = mysql_query("select count(id),rollno from tbl_attendance where batchid = '$Batch' and sessionid = '$Session' GROUP BY rollno");
    $array = array();
    $array_rollno = array();
    $i1 = 0;
    while ($Row_T = mysql_fetch_array($qatt1)) {
        $array[$i1] = $Row_T[0];
        $array_rollno[$i1] = $Row_T[1];
        $i1++;
    }

    $maxValue = max($array);
    $roll_no = $array_rollno;
    foreach($roll_no as $roll) {
        $allRollno[$indexes] = $roll;

        // Absent - Attendance
        $Abatt4 = mysql_query("select count(id) from tbl_attendance where rollno = '$roll' and batchid = '$Batch' and sessionid = '$Session' and attend = 0 GROUP by rollno");

        if ($Row_A4 = mysql_fetch_array($Abatt4)) {
            $allAbsent[$indexes] = round(($Row_A4[0] / $maxValue * 100) , 2);
        } else {
            $allAbsent[$indexes] = 0;
        }

        $indexes++;
    }

    print_r($allAbsent);
    echo "<br /><br />";
    $tabsent = $allAbsent;
    rsort($tabsent);
    foreach($tabsent as $key => $val) {
        echo "[$key] => $val\n";
    }

1 个答案:

答案 0 :(得分:2)

快速的 Google 搜索通常可以解决问题...

arsort($tabsent);

来自manual

  

arsort —以相反的顺序对数组进行排序并保持索引关联