简体   繁体   English

需要通过循环创建降序

[英]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.我有两个 arrays。我正在通过循环比较那些 arrays 并计算结果,但它总是按数组索引进行升序排列。 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 output 是

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

I need the output by descending order with scores.我需要分数降序排列的 output。

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

create an array placeholder for studentId and totalScores then reverse the array is much easier I thinkstudentIdtotalScores创建一个数组占位符然后反转数组我认为更容易

<?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演示: https://3v4l.org/Fgsi9

[edit] I neglect that you require sorting on the score value. [编辑] 我忽略了您需要对score进行排序。 In this case, you need to compare the value from the new array.在这种情况下,您需要比较新数组中的值。

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

more on this sorting based on array key/value here How to Sort a Multi-dimensional Array by Value更多关于这里基于数组键/值的排序如何按值对多维数组进行排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM