Hayam khan
Hayam khan

Reputation: 59

Sort array without replacing index elements in php

How to sort an array without replacing index element? When I descending order sort an absent array then indexing value will be replace, is there any way to descending order sort an array and keep original indexes?

// 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";
    }

Upvotes: 0

Views: 832

Answers (1)

BritishWerewolf
BritishWerewolf

Reputation: 3968

A quick Google search normally does the trick...

arsort($tabsent);

From the manual:

arsort — Sort an array in reverse order and maintain index association

Upvotes: 2

Related Questions