3v4l.org

run code in 300+ PHP versions simultaneously
<?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 => '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 ) );
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Input:Array ( [0] => 1 [1] => 2 [2] => 2 [3] => 0 [4] => -1 [5] => 1 [a] => c [b] => a [c] => c ) array_unique: Array ( [0] => 1 [1] => 2 [3] => 0 [4] => -1 [a] => c [b] => a ) Replacement #1: Array ( [0] => 1 [1] => 2 [2] => 0 [3] => -1 [4] => c [5] => a ) Replacement #2: Array ( [0] => 1 [1] => 2 [3] => 0 [4] => -1 [a] => c [b] => a ) Replacement #3: Array ( [a] => c [b] => a [0] => 1 [4] => -1 [3] => 0 [1] => 2 ) Replacement #4: Array ( [0] => 1 [1] => 2 [2] => 0 [3] => -1 [4] => c [5] => a ) Replacement #5: Array ( [5] => 1 [2] => 2 [3] => 0 [4] => -1 [c] => c [b] => a )

preferences:
261.19 ms | 406 KiB | 373 Q