Faster Alternative to PHP’s Array Unique Function

Overview

Using PHP’s function array_unique() allows you to take an array of values and remove the duplicates leaving you with an array with unique values. This function works best most of the time, however if you find yourself trying to use array_unique on massive arrays, it may slow down quite a bit.

Instead of using array_unique to create a distinct array, there is another nifty array function that will speed things up called `array_flip()`. With this magic function, it swaps each of the array’s key with its value and since keys must be unique, then you will be left with the same result as if you had used array_unique()

Faster Alternative to PHP’s Array Unique Function

/* create our array of 4 elements with duplicate values */
$array = array('green','blue','orange','blue');

/* flip the array now we will have an array with unique keys
	array('green'=>0,'blue'=>1,'orange'=>2); */
$array = array_flip($array);

/* and then flip the array back to put the keys and values where they are meant to be leave us with array(0=>'green',1=>'blue',2=>'orange'); */
$array = array_flip($array);

Since we have now removed a number of elements the array keys may not be in sequence. For example we may be left with array(0=>’A’,2=>’B’,5=>’C’);. In some situations, this is not an issue, however if you rely on the array keys being in numerical sequence, you can one of two things to fix the array keys.

Fix array keys using array_merge()

Add the following after the array flip to sort out the array keys and put them back into sequence, i.e. 0, 1, 2, 3, …

$array = array('green','blue','orange','blue');
$array = array_flip($array);
$array = array_flip($array);

/* Use array_merge() to fix the keys */
$array = array_merge($array);

Second method using array_keys()

Note, this method of fixing the array keys is marginally faster than using the array_merge() method
You could also combine the last step to use array_keys – which at the time would be the flipped values. Then once you flip the values, the keys will be created in order.

$array = array('green','blue','orange','blue');
$array = array_flip($array);
/* Same as first example, except now we first extract array keys */
$array = array_keys($array);

Note: Thanks to yehosef for helping fix and improve the above code.

Conclusion

Easy as that! and can have a significant increase in performance on massive arrays over using array_unique.