Forum Moderators: coopster
if ($a<15 && $b<1.2) {
$scenario='A';
} elseif ($a<23 && $b<2.6) {
$scenario='B';
} elseif ($a<36 && $b<3.4) {
$scenario='C';
} else {
$scenario='D';
}
//And then where you need it:
switch ($scenario) {
case 'A':
echo 'executing A';
break;
case 'B':
echo 'executing B';
break;
default:
echo 'executing other scenario';
break;
}
$data=... your data;
$scenarios=NULL Array() of same size (I assume a 1D array here)
//first test
foreach ($data as $pointer=>$datum)
{
if ($scenarios[$pointer]!==NULL) continue;
if (testA($datum)) $scenarios[$pointer]="A";
}
//second test
foreach ($data as $pointer=>$datum)
{
if ($scenarios[$pointer]!==NULL) continue;
if (testB($datum)) $scenarios[$pointer]="B";
}
//third test
foreach ($data as $pointer=>$datum)
{
if ($scenarios[$pointer]!==NULL) continue;
if (testC($datum)) $scenarios[$pointer]="C";
}
//final test
foreach ($data as $pointer=>$datum)
{
if ($scenarios[$pointer]!==NULL) continue;
$scenarios[$pointer]="D"; //catch all
}
anything that meets test 3 (< some number) automatically meets test 2 (< some smaller number)
anything that meets test 2 (< some number) automatically meets test 3 (< some larger number)
// Declare scenarios (A..H = 8)
$scenarios = array (
'A' => array (15, 1.2), // $a, $b
'B' => array (23, 2.6),
'C' => array (36, 3.4),
// D, E, F, G and H
);
// Example data (large array)
$data = array (
array (10, 1.8), // $a, $b
array (24, 3.0),
array (40, 2.3),
// etc.
);
// Run test to determine which subset of scenarios to test for
// - based on $data
$scenariosToTest = 'ABCD';
$lastScenarioId = $scenariosToTest[strlen($scenariosToTest)-1];
// Step through data and check scenarios
foreach ($data as $row) {
// Step through scenarios to test (subset)
foreach (str_split($scenariosToTest) as $scenarioId) {
// Reached last scenario to test? (ELSE)
// Could combine into check below or omit entirely and allow to fall through if it fails
if ($scenarioId == $lastScenarioId) {
break;
}
// $row[0] is $a / $row[1] is $b from OP's example
if (($row[0] < $scenarios[$scenarioId][0]) && ($row[1] < $scenarios[$scenarioId][1])) {
break;
}
}
// Do something with resulting scenario A, B, C, etc.
echo $scenarioId;
}