Skip to content
Advertisement

Selecting first number not in an array PHP

I have the following problem but I will simplify it here:

JavaScript

Part 1: Give me the first number available? “Answer is 2”

Part 2: Give me the next number available? “Answer is 7”

This is a simplified version of a math problem im facing, im designing a dynamic approval system that modifies the entire program based on changeable rules.

Advertisement

Answer

You could use array_diff with a range from $starting+1 to $ending:

JavaScript

Output:

JavaScript

The output shows that there are still available numbers (you can keep using array_shift to get them) after taking 2 from the array.

Demo on 3v4l.org

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement