所以我有一个

学生模特, 主题模型, MARKS模型,其中包含总分字段

我想要做的是获得班上每个学生的考试总分,然后根据班级中最高分检索结果,然后将这些分数定位为1、2、3……

然后最终得到一个学生的职位,并将其存储在考试记录表中.

到目前为止,我能够实现获得每个学生的考试总分,将它们存储在一个数组中,根据最高分进行排序,

现在唯一让我头疼的问题是从数组中获取这些分数的位置,我的问题是如何获得学生的分数位置,或者是否有方法将位置添加到分数中,然后检索学生的位置

例如

1 student_id  => 2,  total_score => 500 
2  student_id => 3, total_score => 455
3  student_id => 5, total_score =>  345

这是我的代码下面,请任何人有 idea 如何解决这个问题,我需要你的帮助.

TextInput::make('position')->numeric(
            function (Closure $set) {
            // Get all students from class
            $studentsInClass = $this->class->students;
            
            //empty array to store student's total_score
            $totalScore = [];
            
           // loop through students get all their total_score on all subjects from mark table and sum it, then store it 
           in totalScore array.
            foreach ($studentsInClass as $key => $student) {
              
          $totalScore[] = array('student_id' => $student->id, 'total_score' => $student->marks->sum('total_score') );
            }
            // Sort scores from highest to lowest
            $sortedScores= array_values(array_reverse(Arr::sort($totalScore, function ($value) {
                return $value['total_score'];
            })));

            // get the current student Id 
            $id = $this->student->id;
            // find a student in the array that matches the current student id and return his score.
            //so this is where I would like to return the score and position of the student 
            $filteredArray = Arr::where($sortedScores, function ($value, $key) use ($id) {
                return $value['student_id'] == $id;
            });
          
        }
        )->disabled(),

如果您dd($sortedScores)

enter image description here

推荐答案

您有一个二维array.

$sortedScores = [
  ['student_id'  => 2, 'total_score' => 443],
  ['student_id'  => 4, 'total_score' => 410],
  ['student_id'  => 1, 'total_score' => 371],
  ['student_id'  => 3, 'total_score' => 170],
];

每行的索引已经是从0到3的连续索引.我认为你想要增加一个排名.

foreach($sortedScores as $idx => $row)
  $sortedScores[$idx]['rank'] = $idx+1;

//test output
var_export($sortedScores);

输出:

array (
  0 => 
  array (
    'student_id' => 2,
    'total_score' => 443,
    'rank' => 1,
  ),
  1 => 
  array (
    'student_id' => 4,
    'total_score' => 410,
    'rank' => 2,
  ),
  2 => 
  array (
    'student_id' => 1,
    'total_score' => 371,
    'rank' => 3,
  ),
  3 => 
  array (
    'student_id' => 3,
    'total_score' => 170,
    'rank' => 4,
  ),
)

如果希望排名从0开始,只需指定$IDX即可.

试穿https://3v4l.org/9Dv7D

我希望这会有帮助,并希望我正确地理解了您的问题.

要在多维数组中查找特定值的键,请参阅‘PHP Multidimensional Array Searching (Find key by specific value)

Php相关问答推荐

LaravelEloquent 的地方条件父/子与第三模型多对多

从WC会话变量更改WooCommerce checkout 托运方法成本

在Laravel中修改JSON输出

PHP MySQL求和子树并与父树累加

免费送货柜台,考虑到折扣码

如何在php网站代码中清理浏览器缓存

对表示大小的值后缀进行自定义排序(XXS、XS、S、M、L、XL、XXL)

防止 WooCommerce 在前端加载全尺寸图像

如何用十进制和字母数字减go 和添加 id

在 Heroku 上部署 Sylius - 路由不正确

woocommerce checkout 页面上的自定义字段

避免在 WooCommerce 中多次触发挂钩函数

使用其ID查询和捕获与订单相关的其他详细信息的shopware 6方法

Woocommerce注册中的自定义复选框验证错误提示问题

Laravel的Storage::exists返回false,而file_exists返回true.

MySQLI bind_param导致的Cannot modify readonly property错误

如何在光速网络服务器中使用 apache ..htaccess

在 DomPDF 和 Laravel 中使用内联 css

mysql / sql修复多对多数据库中的行

使用 PHP 有条件地使用关键字 Twilio Programmable SMS