Say i have the following array: 1,2,3,6,8
I need to find that the next missing number is 4
What is the easiest and quickest way to perform this ?
Say i have the following array: 1,2,3,6,8
I need to find that the next missing number is 4
What is the easiest and quickest way to perform this ?
Iterating the array and checking in_array()
should do it.
$missing = null;
for ($i = 1; $i <= 10; $i++) {
if (!in_array($i, $myArray)) {
$missing = $i;
break;
}
}
if (!is_null($missing)) {
// hit
}