We help IT Professionals succeed at work.

php while not in_array()

Last Modified: 2020-04-19
I have an account number which basically a STRTOTIME. Now that I think about it was probably not the best idea but its too late I need to work with it for various reasons. In the db the account number is obviously a UNIQUE field. I really never thought to get a duplicate but it happen today.....

I created a function that verifies if the account number exists if it does I simply want to add 1 second.

What I have so far:
1. I got all the account numbers in the system and put them in an array
2. I check if() the newly generated account number is in the array

What I need to do now in the IF() is to create a loop that adds 1 second to the timestamp and check if its in the array.

I tried the following but doesnt seem to work:
	public function create_account_number($account_date, $application_time){
		global $Connection;
		$all_account_numbers = array();
		$query = $Connection->query("SELECT account_number FROM accounts");
		while($result = mysqli_fetch_array($query, MYSQLI_ASSOC)){
			array_push($all_account_numbers, $result['account_number']);
//		echo "<pre>";
//		print_r($all_account_numbers);
//		echo "</pre>";
		$account_number = strtotime($account_date . " " . $application_time);
		if(in_array($account_number, $all_account_numbers)){
			echo "In Array <br />";
			while(in_array(strtotime('+1 SECOND', $account_number), $all_account_numbers)){ 
				$account_number = strtotime('+1 SECOND', $account_number);
		return $account_number;

Open in new window

Watch Question


Don't bother using strtotime to add one second to a timestamp. A timestamp is just a number of seconds anyway, so just add 1:

while(in_array(strtotime('+1 SECOND', $account_number), $all_account_numbers)){ 
	$account_number = strtotime('+1 SECOND', $account_number);

Open in new window

while(in_array($account_number, $all_account_numbers)){ 

Open in new window

On a side note, it's never "too late" to change a bad data model. If you know of a foundational problem with your data like this, then the best time to fix it is ASAP. Otherwise, if you let it go, it will simply get worse and become even harder every day you let the problem fester.

Even this "fix" is burying you a little deeper in inefficiency that you'll have to strip out some day. It pulls all the account numbers into memory, which means you're doing a full query of those accounts, so the query will only get bigger and take up more memory as time progresses. If someday it hits the memory limit, then suddenly this process will break without any warning.

This is generally why auto increment columns are useful at the beginning. They can give you a unique new ID and that model will work for a good long while, and it can even work for a short time once you reach the point of multiple DB servers.
This one is on us!
(Get your first solution completely free - no credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.