I want to loop only if points are greater than levels. The result I want is "reward0"
The sum of points is 80 so greater than 50 but less than 100 so only one reward is required.
I tried like this but no luck.
$levels = [50, 100, 150, 200];
$points = 0;
$rewards = [];
foreach ($levels as $key => $level) {
if($points >= $level) {
$rewards[] = 'reward' . $key;
}
// 20, 40, 60, 80
$points += 20;
}
return $rewards;
no luck. Do you get an error? Is this in a function?returnmaybe should beprint_r? Also not clear aboutpoints is 80 so greater than 50 but less than 100if condition. That is, theifcondition never becomes true. Thus$rewardsarray will be always empty.