= (int)$matches[0][$i] and $number <= (int)$matches[1][$i]) || ($number >= (int)$matches[2][$i] and $number <= (int)$matches[3][$i]); if ($comparison === true) { return true; } return $number; }; } return $compares; } $compares = parseInstructions($instructions); // check others $others = explode("\n", $others); array_shift($others); $others = array_map(static function($other) { return explode(',', $other); }, $others); $sum = 0; foreach ($others as $other) { foreach ($other as $number) { $is_valid = false; foreach ($compares as $compare) { if ($compare((int)$number) === true) { $is_valid = true; break; } } if (!$is_valid) { $sum += $number; echo sprintf('%u not in comparison list%s', $number, "\n"); } } } // sum it up echo $sum;