Let's say I have an array of usernames as such:
[
'john.doe',
'john.doe1',
'john.doe2',
'john.doe4',
'john.doe5'
]
Now a new user registers as john.doe. I have fetched the already existing john.doe's from the DB using a LIKE query and stored in the array above. Then I want to iterate over that array to see which spot is available for the newly registered john.doe.
In this case the available spot would be john.doe3.
I know how to do this using a while loop and simply adding an increment until there's no match.
Basically I have two questions:
Is this the right way of approaching this duplicate username issue altogether?
I was wondering if maybe there's a better way in lieu of using the while loop?