Home > Blockchain >  Assigning a string value if input value falls within a specific range in Laravel 8
Assigning a string value if input value falls within a specific range in Laravel 8

Time:02-13

I compare an input value to multiple range of values to determine the string value for another variable. The below code works, but I am wondering if there is a more elegant solution?

...
if ($totalPoints >= 0 && $totalPoints <= 249) {
    $carClass = 'CC01';
} elseif ($totalPoints >= 250 && $totalPoints <= 299) {
    $carClass = 'CC02';
} elseif ($totalPoints >= 300 && $totalPoints <= 349) {
    $carClass = 'CC03';
} elseif ($totalPoints >= 350 && $totalPoints <= 399) {
    $carClass = 'CC04';
} elseif ($totalPoints >= 400 && $totalPoints <= 449) {
    $carClass = 'CC05';
} elseif ($totalPoints >= 450 && $totalPoints <= 499) {
    $carClass = 'CC06';
} elseif ($totalPoints >= 500 && $totalPoints <= 549) {
    $carClass = 'CC07';
} elseif ($totalPoints >= 550 && $totalPoints <= 599) {
    $carClass = 'CC08';
} elseif ($totalPoints >= 600 && $totalPoints <= 649) {
    $carClass = 'CC09';
} elseif ($totalPoints >= 650 && $totalPoints <= 699) {
    $carClass = 'CC10';
} elseif ($totalPoints >= 700 && $totalPoints <= 749) {
    $carClass = 'CC11';
} elseif ($totalPoints >= 750 && $totalPoints <= 824) {
    $carClass = 'CC12';
} elseif ($totalPoints >= 825 && $totalPoints <= 899) {
    $carClass = 'CC13';
} elseif ($totalPoints >= 900 && $totalPoints <= 974) {
    $carClass = 'CC14';
} elseif ($totalPoints >= 975 && $totalPoints <= 1049) {
    $carClass = 'CC15';
} elseif ($totalPoints >= 1050 && $totalPoints <= 1149) {
    $carClass = 'CC16';
} elseif ($totalPoints >= 1150 && $totalPoints <= 1249) {
    $carClass = 'CC17';
} elseif ($totalPoints >= 1250) {
    $carClass = 'CC18';
}
...

CodePudding user response:

This might be a more compact solution:

function findInRange($number, $array)
{
    foreach ($array as $key => $value) {
        list($min, $max) = explode('-', $key);
        if (
            $number >= $min && $number <= $max) {
            return $value;
        }
    }

    return null;
}

$totalPoints = 1300;

$carClassArray = [
    "0-249" => 'CC01',
    "250-299" => 'CC02',
    "300-349" => 'CC03',
    "250-399" => 'CC04',
    "400-449" => 'CC05',
    "450-499" => 'CC06',
    "500-549" => 'CC07',
    "550-599" => 'CC08',
    "600-649" => 'CC09',
    "650-699" => 'CC10',
    "749-1000000" => 'CC11',
];
$carClass = findInRange($totalPoints, $carClassArray);

echo $carClass;

CodePudding user response:

Based on your code I think that the $carClass always has the format CCXX where XX is two digits number and starts from 0 to the ranges count. So you can use the 2nd value for each range and add them to the array and get the index of the desired value after sorting the array

$totalPoints = 290;
// -1 to get CC01 for 0 value
$breaks = [-1, 249, 299, 349, 399, 449]; // add all the 2nd values
$breaks[] = $totalPoints;
asort($breaks);
$breaks = array_values($breaks);
$index = array_search($totalPoints, $breaks);
$carClass = 'CC' . str_pad($index, 2, '0', STR_PAD_LEFT);
echo $carClass; // CC02

CodePudding user response:

This can be a way:

$totalPoints = 399;

$rangeArray = [
    [0, 249, 'CC01'],
    [250, 299, 'CC02'],
    [300, 349, 'CC03'],
    [350, 399, 'CC04'],
    /* .... More range */
    [12520, null, 'CC18']
];

foreach($rangeArray as $range){
    if( ($totalPoints >= $range[0]) && ($range[1] ? ($totalPoints <= $range[1]) : true) ){
        $carClass = $range[2];
        break;
    }
}
echo $carClass;// CC04
  • Related