Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7856607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:45:35+00:00 2026-06-02T20:45:35+00:00

I have two arrays; $marks and $grades . $marks contain mark scored by a

  • 0

I have two arrays; $marks and $grades. $marks contain mark scored by a student and $grades is obtained by looping through $marks using the following function.

 function convertMarkToGrade($mark)
    {
        if($mark<21)
            return "D";
        else if($mark<33)
            return "C";
        else if($mark<41)
            return "B";
        else if($mark<=50)
            return "A";
    }

The problem is i want to upgrade the smallest and second smallest grades in $grades array using the following criteria

  1. Upscaling is done from lowest grade to next higher grade and so on ie B to A, C to B etc

  2. In case of tie in Grades the grade with highest mark is upgraded.

For example:
Let $marks be array(25,43,36,16,28). So we get $grades as array("C","A","B","D","C"). i want to generate a $upgraded_grades =array("C","A","B","C","B") ie the D grade (the smallest grade) is upgraded and the C grade(second smallest grade but with maximum marks) is also upgraded.

How can I do it in php?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:45:37+00:00Added an answer on June 2, 2026 at 8:45 pm

    You can use Associative arrays. Here is a one of the method:

    Edited:

    //create associative array that has values as array of marks
    $grades_arr = array("D" => array(), "C" => array(), "B" => array(), "A" => array() );
    
    for($i = 0;$i < count($grades);$i++)
        array_push($grades_arr[$grades[$i]], $marks[$i]);
    
    $loop = 0;
    foreach($grades_arr as $key => &$value){
        if(empty($value))
            continue;
    
        if($loop == 2)
            break; //break after two upgrades
        else{
            $max = max($value);
            //unset the variable
            $value = array_diff($value, array($max));
            $value = array_values($value); 
            //push it into next higher grade
            array_push($grades_arr[chr(ord($key)-1)], $max);
            $loop++;
        }
    }
    
    print_r($grades_arr);
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays which contain times. The first is the state of a
I have two arrays. Each is being pulled from MySQL via the following; $query
I have two arrays, one is very large (more than million entries) and other
I have two arrays with time values in them in this format. 00:00:00 which
I have two arrays of values like X,Y,Z and 1,2 There is a table
I have two arrays that I would like to compare and ultimately wind up
I have two arrays: Walls and Neighbors. public boolean[] walls = new boolean[4]; public
I have two arrays (a and b) with n integer elements in the range
I have two arrays: $course = [ 6 => 24, 0 => 20, 1
I have two arrays. I'd like to merge them into one, but remove the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.