Home > Enterprise >  How do I get the position of an element in an array in php/laravel
How do I get the position of an element in an array in php/laravel

Time:01-05

So I have a

student model, subject model, marks model, which has the total score field

what I am trying to do is get the total score in an exam for each student in a class and then retrieve the results according to the highest score in the class and then position those scores as 1,2,3...

then finally get the position a single student and store it in an exam record table.

so far I was able to achieve getting the total score in an exam for each student, stored them in an array, sorted them according to the highest score,

the only issue now giving me a headache is getting the position of those scores from the array, my question is how can I get the position of a score for a student, or is there a way to add the positions to the scores and then retrieve the position for a student

For example

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

here is my code below, Please anyone with an idea how to solve this I need your help.

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(),

if you dd($sortedScores)

enter image description here

CodePudding user response:

You have a two-dimensional 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],
];

The index of each row is already consecutive from 0-3. I think you want to add a rank to the rows.

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

//test output
var_export($sortedScores);

Output:

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,
  ),
)

If you want rank to start at 0, just assign $idx.

Try on https://3v4l.org/9Dv7D

I hope this helps and that I understood your problem correctly.

To find the key for a specific value in a multidimensional array see 'PHP Multidimensional Array Searching (Find key by specific value)'

CodePudding user response:

You can use the array_search() function. This will either return the key of the value you are looking for or false if it is not in the array.

$letters = ['a', 'b', 'c', 'd'];
$key = array_search('b', $letters);
//In this case $key will equal to 2.

CodePudding user response:

Presuming that $filteredArray contains a single item, then the first key should be the student's position in the sorted array.

$position = array_keys($filteredArray)[0];
  • Related