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 8091233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:59:56+00:00 2026-06-05T19:59:56+00:00

Problem: I have a value that is set in $record, for instance 1.69. Then

  • 0

Problem:

I have a value that is set in $record, for instance 1.69. Then I have an array that contain different grades and values. I would like to compare $record against the highest value first to see if it is higher or equal to, if not go to the value below it, and so forth.

PHP code:

$record1 = '1.69';
$record2 = '2.90';
$record3 = '3.40';
$record4 = '3.80';

Array ($grades):

Array
(
    [G] => 2.8
    [VG] => 3.8
)

Scenario:

$record1 should be compared to the highest value in the array, this will return false. It will compare to the value below the highest value, which also will return false. If both return false then return the string ‘U’, otherwise return the key G or VG.

  • Record 1 should produce U.
  • Record 2 should produce G.
  • Record 3 should produce G.
  • Record 4 should produce VG.

Question:

I could do a lot of if-statements as a solution but I wonder if there is any clever way of doing this check in a better way?

  • 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-05T19:59:57+00:00Added an answer on June 5, 2026 at 7:59 pm

    Using simple logic:

    $grades  = array('G' => 2.8, 'VG' => 3.8);
    $records = array(1.69, 2.9, 3.4, 3.8);
    
    foreach ($records as $v) {
        $res = ($v < $grades['G'] ? 'U' : ($v < $grades['VG'] ? 'G' : 'VG'));
        echo "$v = $res\n";
    }
    
    /*
    1.69 = U
    2.9 = G
    3.4 = G
    3.8 = VG
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with cakephp's Session->write method. If I set a value like $_SESSION['..']
The problem I'm having is that I have a default value in a field,
I have a textbox that gets a decimal value say 10500.00 the problem is
I have the problem, that MSSQL Server 2000 should select some distinct values from
I have the following problem. I have a data set that has the beginning
I have to create a report that will compare values from different years. The
I have problem with getting value of parameter when I have variable with some
I have a problem with setting value to NSString inside block. __block NSString *cityID
I have a problem while converting a string whose value is dd.mm.yyyy to DateTime
i have a problem in inserting the listbox value into mysql database in vb

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.