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 => '', 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 ) );
Output for 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.15, 8.4.1 - 8.4.2
Input:Array ( [0] => 1 [1] => 2 [2] => 2 [3] => 0 [4] => -1 [5] => [6] => [7] => Test [8] => 0.1 [9] => 1 [a] => c [b] => a [c] => c ) array_unique: Array ( [0] => 1 [1] => 2 [3] => 0 [4] => -1 [5] => [7] => Test [8] => 0.1 [a] => c [b] => a ) Replacement #1: Deprecated: Implicit conversion from float 0.1 to int loses precision in /in/ZeLCf on line 12 Array ( [0] => 1 [1] => 2 [2] => 0 [3] => -1 [4] => [5] => Test [6] => c [7] => a ) Replacement #2: Deprecated: Implicit conversion from float 0.1 to int loses precision in /in/ZeLCf on line 22 Array ( [0] => 1 [1] => 2 [3] => 0 [4] => -1 [5] => [7] => Test [a] => c [b] => a ) Replacement #3: Warning: array_flip(): Can only flip string and integer values, entry skipped in /in/ZeLCf on line 32 Warning: array_flip(): Can only flip string and integer values, entry skipped in /in/ZeLCf on line 32 Array ( [a] => c [b] => a [0] => 1 [7] => Test [5] => [4] => -1 [3] => 0 [1] => 2 ) Replacement #4: Warning: array_flip(): Can only flip string and integer values, entry skipped in /in/ZeLCf on line 38 Warning: array_flip(): Can only flip string and integer values, entry skipped in /in/ZeLCf on line 38 Array ( [0] => 1 [1] => 2 [2] => 0 [3] => -1 [4] => [5] => Test [6] => c [7] => a ) Replacement #5: Warning: array_flip(): Can only flip string and integer values, entry skipped in /in/ZeLCf on line 44 Warning: array_flip(): Can only flip string and integer values, entry skipped in /in/ZeLCf on line 44 Array ( [9] => 1 [2] => 2 [3] => 0 [4] => -1 [5] => [7] => Test [c] => c [b] => a )
Output for 8.0.0 - 8.0.30
Input:Array ( [0] => 1 [1] => 2 [2] => 2 [3] => 0 [4] => -1 [5] => [6] => [7] => Test [8] => 0.1 [9] => 1 [a] => c [b] => a [c] => c ) array_unique: Array ( [0] => 1 [1] => 2 [3] => 0 [4] => -1 [5] => [7] => Test [8] => 0.1 [a] => c [b] => a ) Replacement #1: Array ( [0] => 1 [1] => 2 [2] => 0 [3] => -1 [4] => [5] => Test [6] => c [7] => a ) Replacement #2: Array ( [0] => 1 [1] => 2 [3] => 0 [4] => -1 [5] => [7] => Test [a] => c [b] => a ) Replacement #3: Warning: array_flip(): Can only flip string and integer values, entry skipped in /in/ZeLCf on line 32 Warning: array_flip(): Can only flip string and integer values, entry skipped in /in/ZeLCf on line 32 Array ( [a] => c [b] => a [0] => 1 [7] => Test [5] => [4] => -1 [3] => 0 [1] => 2 ) Replacement #4: Warning: array_flip(): Can only flip string and integer values, entry skipped in /in/ZeLCf on line 38 Warning: array_flip(): Can only flip string and integer values, entry skipped in /in/ZeLCf on line 38 Array ( [0] => 1 [1] => 2 [2] => 0 [3] => -1 [4] => [5] => Test [6] => c [7] => a ) Replacement #5: Warning: array_flip(): Can only flip string and integer values, entry skipped in /in/ZeLCf on line 44 Warning: array_flip(): Can only flip string and integer values, entry skipped in /in/ZeLCf on line 44 Array ( [9] => 1 [2] => 2 [3] => 0 [4] => -1 [5] => [7] => Test [c] => c [b] => a )
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
Input:Array ( [0] => 1 [1] => 2 [2] => 2 [3] => 0 [4] => -1 [5] => [6] => [7] => Test [8] => 0.1 [9] => 1 [a] => c [b] => a [c] => c ) array_unique: Array ( [0] => 1 [1] => 2 [3] => 0 [4] => -1 [5] => [7] => Test [8] => 0.1 [a] => c [b] => a ) Replacement #1: Array ( [0] => 1 [1] => 2 [2] => 0 [3] => -1 [4] => [5] => Test [6] => c [7] => a ) Replacement #2: Array ( [0] => 1 [1] => 2 [3] => 0 [4] => -1 [5] => [7] => Test [a] => c [b] => a ) Replacement #3: Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/ZeLCf on line 32 Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/ZeLCf on line 32 Array ( [a] => c [b] => a [0] => 1 [7] => Test [5] => [4] => -1 [3] => 0 [1] => 2 ) Replacement #4: Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/ZeLCf on line 38 Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/ZeLCf on line 38 Array ( [0] => 1 [1] => 2 [2] => 0 [3] => -1 [4] => [5] => Test [6] => c [7] => a ) Replacement #5: Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/ZeLCf on line 44 Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/ZeLCf on line 44 Array ( [9] => 1 [2] => 2 [3] => 0 [4] => -1 [5] => [7] => Test [c] => c [b] => a )

preferences:
79.86 ms | 418 KiB | 5 Q