Search code examples
phploopssql-order-by

Need to create descending order through loop


I have two arrays. I am comparing those arrays through loop and calculating results but It is always making ascending order by the array indexes. I need to create order after calculation with those values in descending order.

<?php
$correctAnswers = [
['name'=> 1, 'value'=> 4],
['name'=> 1, 'value'=> 1]
];

$submittedStudentData =[
[101, 0, 1, 1],
[102, 0, 4, 1]
];

if(!is_array($submittedStudentData))
{
    $submittedStudentData = json_decode($submittedStudentData);
}


for ($index = 0; $index < count($submittedStudentData); $index++){

   // get each student data
   $studentData = $submittedStudentData[$index];

   // get the student roll number
   $studentId = $studentData[0];


   // initialize the total result with zero pre value
   $totalResult = 0;

// loop through the student's submitted answer
for ($i = 2; $i < count($studentData); $i++) 
{
   if ((int)$studentData[$i] === (int)$correctAnswers[$i - 2]["value"])
  {
    $totalResult++;
  }
}


// final result for this student
echo 'Student with roll number '. $studentId .' scores '. $totalResult."\n";


}
?>

The output is

Student with roll number 101 scores 1
Student with roll number 102 scores 2

I need the output by descending order with scores.

Student with roll number 102 scores 2
Student with roll number 101 scores 1

Solution

  • create an array placeholder for studentId and totalScores then reverse the array is much easier I think

    <?php
    $correctAnswers = [
    ['name'=> 1, 'value'=> 4],
    ['name'=> 1, 'value'=> 1]
    ];
    
    $submittedStudentData =[
    [101, 0, 1, 1],
    [102, 0, 4, 1]
    ];
    
    if(!is_array($submittedStudentData))
    {
        $submittedStudentData = json_decode($submittedStudentData);
    }
    
    $s_data = [];
    for ($index = 0, $c = count($submittedStudentData); $index < $c; $index++){
    
       // get each student data
       $studentData = $submittedStudentData[$index];
    
       // get the student roll number
       $studentId = $studentData[0];
    
    
       // initialize the total result with zero pre value
       $totalResult = 0;
    
    // loop through the student's submitted answer
    for ($i = 2, $x = count($studentData) ; $i < $x; $i++) 
    {
       if ((int)$studentData[$i] === (int)$correctAnswers[$i - 2]["value"])
      {
        $totalResult++;
      }
    }
    
        $s_data[] = ['studentId' =>$studentId, 'scores'=>$totalResult];
    
    // final result for this student
    
    }
    
    $r = array_reverse($s_data);
    
    foreach($r as $d)
    {
        echo 'Student with roll number '. $d['studentId'] .' scores '. $d['scores']."\n";
    
    }
    ?>
    

    demo: https://3v4l.org/Fgsi9

    [edit] I neglect that you require sorting on the score value. In this case, you need to compare the value from the new array.

    demo: https://3v4l.org/amfBa

    more on this sorting based on array key/value here How to Sort a Multi-dimensional Array by Value