1

Background

I am creating a ranking system which retrieves a bunch of records to compare their grades, put it on rank and delete that record for the next comparison. However, I'm having trouble on how to delete the record I tried unset() but it does not seems to work either.

Question

This the code that I'm using. Please take note that this is just pseudo-code of what we are doing and is not the actual code just to avoid confusions from the question. Take a look at this code:

// Retrive all the student records with grades.
$students = $this->grades->RetrieveRecords();

// Occupy slot.
$iterator=0;
$highest_index =0 ;
for($i=0;$i<5;$i++){

    // Search student for rank $i.

    foreach($students as $student)
    {
        // Some comparisons

        // consider we found the highest yet.
        if($highest<$student['grade']){

            // Store which index it is, because it will be deleted
            // on the next cycle if this $student['grade'] is indeed the highest on this cycle.
            $highest_index = $iterator;
        }
        $iterator+=1;
    }   

    // After getting the highest for rank $i. Delete that current record
    // from $students so on next cycle, it will be removed from the comparison.

    $unset($students[$highest_index]); // Does not work, any alternative? - Greg

    // Reset the foreach iterator for next comparison cycle.
    $iterator=0;

The $unset($students[$highest_index]); is the one we need to do the job but doesn't. We just need to delete a specific record from result_array() which is the $students. For now we ran out of alternatives and still searching across the internet/documentations. However, I'll just leave this here for some help.

We will also update this if ever we got a solution for the few hours.

1
  • At this point, I'm currently looking into changing methods upon reading Rank average with PHP. Commented Sep 17, 2018 at 23:31

1 Answer 1

1

you can use array_filter:

$students = array_filter($students, function($student) use($highest)
{
    return $student['grade'] < $highest;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.