Avatar of impressionexpress
impressionexpress asked on

php while not in_array()

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


Avatar of undefined
Last Comment

8/22/2022 - Mon

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.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck