Jump to content

limitations on array_unique()


ToonMariner

Recommended Posts

Not blowing my own but I dont think so...

 

here is the code I'm trying to generate the 250000 unique strings...

 

<?php
$chars		= "ABCDEFGHJKLMNPQRSTUVWXYZ";
$charend	= (strlen ( $chars ) - 1);

$date		= date ( 'md' );

$month = $date[0].$date[1];
$year = $date[2].$date[3];

$datecode	=	$chars[$month] . $chars[$year[0]] . $chars[$year[1]];
$uniquecodes	=	array();
$no_codes	=	isset( $_GET['no_codes'] ) ? $_GET['no_codes'] : 250000;

$codecount	=	0;

do
{
$codes		=	array();
for ( $j = $codecount ; $j < $no_codes ; $j++ )
{
	$trackcode = NULL;
	for ( $i = 0 ; $i < 7 ; $i++ )
	{
		$x = rand( 0 , $charend );
		$trackcode	.= $chars[rand( 0 , $charend )];
	}
	$codes[]	=	$trackcode . $datecode;
}
$unique		=	array_merge ( $codes , $uniquecodes );
$uniquecodes	=	array_unique ( $unique );
$codecount	=	count ( $uniquecodes );

if	(
	(time() - $start) > 60
	)
{
	break; // just in to break out of infite loop in dev.
}

} while ( $codecount < ($no_codes) - 1 );
echo 'Unique Codes: ' . $codecount . PHP_EOL;
?>

the date portion (last three characters) is used to keep keys separated by date - these are to be added to a database and i can check if any existing codes end in the same three characters (if they do then I'll manage that elsewhere).

so essentially a 7 letter random string shouold yield 24^7 combos (I omitt I and O so as not to confuse with numbers) which works out at 4,500,000,000 so there should be plenty of room for manouvre.

Could this be a memory limit issue or am I missing something much more fundamental?

Cheers peeps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.