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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:10:39+00:00 2026-05-13T11:10:39+00:00

$arr = $results->row(); $score = 0; foreach ($arr as &$i) { switch($i){ case ($i

  • 0
$arr = $results->row();
$score = 0;
foreach ($arr as &$i) {
    switch($i){
        case ($i <= 4 AND $i >= 1):
            $score += 1;
            break;
        case ($i < 8 AND $i > 4):
            $score += 3;
            break;
        case ($i >= 8):
            $score += .5;
            break;
    }
}
    echo $score

This is my current code that adds up a total value based on about 30 db (SQL) entries. Kind of like a grading ruberik.
I need help improving the above code. Is there a better way to do this?

Also. Some of these (ie, rows 3,5,8 need to be scored differently.) How do I omit these rows in this logic and pass them to be scored differently?

This is a codeigniter function, so I’d love it if somebody could show me a quick easy, pretty way to do this! Thanks a lot!

  • 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-05-13T11:10:39+00:00Added an answer on May 13, 2026 at 11:10 am

    Minor improvements can be made but nothing drastic.

    • Simply using if/else instead of switch case can slightly improve speed.
    • Determine which of the cases is going to occur the most often and place it first, as this will reduce the number of cases needed to be hit for each iteration.
    • Create an array of any iterations you wish to skip for an easy isset call as opposed to checking for each case (multiple if/else statements would be required)
    • Keep track of the number of iterations for skipping those rows

    $skip_iterations = array(3 => 1, 5 => 1, 8 => 1);
    $arr = $results->row();
    $score = 0;
    $i = 1;
    foreach ($arr as &$a) {
        // skip rows 3, 5, and 8
        if (isset($skip_iterations[$i++])) 
            continue;
        if ($a >= 1 && $a <= 4) {
            $score += 1;
        } else if ($a > 4 && $a < 8) {
            $score += 3;
        } else if ($a > 8) {
            $score += .5;
        }
    }
    echo $score;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I take this code <script> var arr = [<%= myArray %>+<%= my2Array
I have a const char arr[] parameter that I am trying to iterate over,
In the code below For i = LBound(arr) To UBound(arr) What is the point
You can initialize an array like this: int [ ] arr = { 1,
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
This program adds two matrices but it's giving too many errors and I can't
I hope someone can help me with the following... I have this code below
I have this function $(this).each(function(index) { arr[index] = ($(this).attr('id')); console.log(arr[index]); fullId[index] = #+arr.shift(); console.log(fullId[index]);
I tried 2 things: (pseudo code below) int arr[10000]; for (int i = 0;
I have an array ( arr ) of elements, and a function ( f

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.