If your key generator has a much larger range of possible values than mere millions there shouldn't be many duplicates, but sooner or later every key is going to have to be checked against every other key to ensure they're not duplicates.
You can use PHP's internal hashtable array lookup to do the checking.
$keys = array();
for($i=0; $i<$number_of_keys_wanted; $i++) $keys[generate_key()] = true;
while(count($keys)<$number_of_keys_wanted) $keys[generate_key()] = true;
Could you explain what you're after in a bit more detail? Like, where does the database come in? Where do you get your keys from, and why do you need so many all at once? If you've already got millions of keys assigned and want to assign another one then the "generate/check" cycle would be the way to go - again, if the pool of possible keys is much larger than you need there shouldn't be much trouble. Like, if you have a billion possible keys and you've assigned a million of them, the chances of generating one that's already assigned and therefore having to generate another is one in a thousand; and the chances are less than one in a million that you'll have to do that twice. On the other hand, if you've only got a million possible keys and you've assigned 750,000 - you've got a 3/4 chance of picking a key that's already assigned, and a 9/16 chance of doing it twice.