I have the following array:
$groupA= array(1,10);
$groupB = array(11,20);
$groupC = array(21,30);
The user has the possibility to enter any numeric value into a text-box, for example "5" now I need to display the user in which group that number is. I've done this before this way:
And then do a switch case like this:
switch ($input){
case ($input>= $groupA[0] && $input<= $groupA[1]):
echo "You are in Group A.";
break;
case ($input>= $groupB[0] && $input<= $groupB[1]):
echo "You are in Group B.";
break;
However, this seems not feasable since we have got many many groups (likely over 200) and using this many switch-cases is unefficient.
Any ideas on how to solve this more elegantly?
I'd make an array:
$groups = array();
$groups['groupA'] = array('min'=>1,'max'=>100);
$groups['groupB'] = array('min'=>1,'max'=>100);
And then
foreach($groups as $label => $group)
{
if($input >= $group['min'] && $input <= $group['max'])
{
echo "You are in group $label";
break;
}
}
or you can put them in a database
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With