After retrieving a list of integers used for ID in a mysql database, taking in that all the ID doesn't follow each other in each case (for example list could be [1,2,3,5,10,11,12,20,...]), what would be an more efficient way, aside from looping through all the integers, to find the lowest integer which isn't yet in the list (in our case, it would be 4, then 6 once 4 is attributed). Also it shouldn't be higher than 999.

[This question](https://stackoverflow.com/questions/2640335/insert-the-lowest-possible-unique-positve-integer-in-an-atomic-fashion-in-mysql) give a mysql query, but I would like to do it in my php script, except if it would be more efficient.