<?php
//----------------------------------
// array_unique() semi-replacements
//----------------------------------
// @link http://php.net/manual/en/function.array-unique.php#98453
function wp_array_unique1( $arr = array() )
{
$tmp = array();
foreach( $arr as $key => $val ) {
$tmp[$val] = true;
}
return array_keys( $tmp );
}
// @link http://www.puremango.co.uk/2010/06/fast-php-array_unique-for-removing-duplicates/#comment-202704
function wp_array_unique2( $arr = array() )
{
$tempArray = array();
foreach($arr as $key => $val ) {
if( isset( $tempArray[$val] ) ) continue;
$tempArray[$val] = $key;
}
return array_flip($tempArray);
}
// @link http://stackoverflow.com/questions/8321620/array-unique-vs-array-flip
function wp_array_unique3( $arr = array() )
{
// Code documented at: http://www.puremango.co.uk/?p=1039
return array_flip(array_flip(array_reverse($arr,true)));
}
// @link http://stackoverflow.com/questions/8321620/array-unique-vs-array-flip
function wp_array_unique4( $arr = array() )
{
return array_keys( array_flip( $arr ) );
}
// @link http://stackoverflow.com/questions/8321620/array-unique-vs-array-flip
function wp_array_unique5( $arr = array() )
{
return array_flip( array_flip( $arr ) );
}
//----------
// Input
//----------
$input = array(
0 => 1,
1 => 2,
2 => 2,
3 => 0,
4 => -1,
5 => '',
6 => null,
7 => 'Test',
8 => 0.1,
9 => '1',
'a' => 'c',
'b' => 'a',
'c' => 'c'
);
//-------------------
// Test comparisions
//-------------------
echo 'Input:';
print_r( $input );
echo 'array_unique: ';
print_r( array_unique( $input ) );
echo 'Replacement #1: ';
print_r( wp_array_unique1( $input ) );
echo 'Replacement #2: ';
print_r( wp_array_unique2( $input ) );
echo 'Replacement #3: ';
print_r( wp_array_unique3( $input ) );
echo 'Replacement #4: ';
print_r( wp_array_unique4( $input ) );
echo 'Replacement #5: ';
print_r( wp_array_unique5( $input ) );
preferences:
74.09 ms | 408 KiB | 5 Q