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 ) );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AaU1X
function name:  (null)
number of ops:  48
compiled vars:  !0 = $input
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   ASSIGN                                                   !0, <array>
   65     1        ECHO                                                     'Input%3A'
   66     2        INIT_FCALL                                               'print_r'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                                 
   67     5        ECHO                                                     'array_unique%3A+'
   68     6        INIT_FCALL                                               'print_r'
          7        INIT_FCALL                                               'array_unique'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $3      
         10        SEND_VAR                                                 $3
         11        DO_ICALL                                                 
   69    12        ECHO                                                     'Replacement+%231%3A+'
   70    13        INIT_FCALL                                               'print_r'
         14        INIT_FCALL                                               'wp_array_unique1'
         15        SEND_VAR                                                 !0
         16        DO_FCALL                                      0  $5      
         17        SEND_VAR                                                 $5
         18        DO_ICALL                                                 
   71    19        ECHO                                                     'Replacement+%232%3A+'
   72    20        INIT_FCALL                                               'print_r'
         21        INIT_FCALL                                               'wp_array_unique2'
         22        SEND_VAR                                                 !0
         23        DO_FCALL                                      0  $7      
         24        SEND_VAR                                                 $7
         25        DO_ICALL                                                 
   73    26        ECHO                                                     'Replacement+%233%3A+'
   74    27        INIT_FCALL                                               'print_r'
         28        INIT_FCALL                                               'wp_array_unique3'
         29        SEND_VAR                                                 !0
         30        DO_FCALL                                      0  $9      
         31        SEND_VAR                                                 $9
         32        DO_ICALL                                                 
   75    33        ECHO                                                     'Replacement+%234%3A+'
   76    34        INIT_FCALL                                               'print_r'
         35        INIT_FCALL                                               'wp_array_unique4'
         36        SEND_VAR                                                 !0
         37        DO_FCALL                                      0  $11     
         38        SEND_VAR                                                 $11
         39        DO_ICALL                                                 
   77    40        ECHO                                                     'Replacement+%235%3A+'
   78    41        INIT_FCALL                                               'print_r'
         42        INIT_FCALL                                               'wp_array_unique5'
         43        SEND_VAR                                                 !0
         44        DO_FCALL                                      0  $13     
         45        SEND_VAR                                                 $13
         46        DO_ICALL                                                 
         47      > RETURN                                                   1

Function wp_array_unique1:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/AaU1X
function name:  wp_array_unique1
number of ops:  14
compiled vars:  !0 = $arr, !1 = $tmp, !2 = $val, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV_INIT                                        !0      <array>
   10     1        ASSIGN                                                   !1, <array>
   11     2      > FE_RESET_R                                       $5      !0, ->8
          3    > > FE_FETCH_R                                       ~6      $5, !2, ->8
          4    >   ASSIGN                                                   !3, ~6
   12     5        ASSIGN_DIM                                               !1, !2
          6        OP_DATA                                                  <true>
   11     7      > JMP                                                      ->3
          8    >   FE_FREE                                                  $5
   14     9        INIT_FCALL                                               'array_keys'
         10        SEND_VAR                                                 !1
         11        DO_ICALL                                         $9      
         12      > RETURN                                                   $9
   15    13*     > RETURN                                                   null

End of function wp_array_unique1

Function wp_array_unique2:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 8
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/AaU1X
function name:  wp_array_unique2
number of ops:  17
compiled vars:  !0 = $arr, !1 = $tempArray, !2 = $val, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV_INIT                                        !0      <array>
   20     1        ASSIGN                                                   !1, <array>
   21     2      > FE_RESET_R                                       $5      !0, ->11
          3    > > FE_FETCH_R                                       ~6      $5, !2, ->11
          4    >   ASSIGN                                                   !3, ~6
   22     5        ISSET_ISEMPTY_DIM_OBJ                         0          !1, !2
          6      > JMPZ                                                     ~8, ->8
          7    > > JMP                                                      ->3
   23     8    >   ASSIGN_DIM                                               !1, !2
          9        OP_DATA                                                  !3
   21    10      > JMP                                                      ->3
         11    >   FE_FREE                                                  $5
   25    12        INIT_FCALL                                               'array_flip'
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                         $10     
         15      > RETURN                                                   $10
   26    16*     > RETURN                                                   null

End of function wp_array_unique2

Function wp_array_unique3:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AaU1X
function name:  wp_array_unique3
number of ops:  13
compiled vars:  !0 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV_INIT                                        !0      <array>
   32     1        INIT_FCALL                                               'array_flip'
          2        INIT_FCALL                                               'array_flip'
          3        INIT_FCALL                                               'array_reverse'
          4        SEND_VAR                                                 !0
          5        SEND_VAL                                                 <true>
          6        DO_ICALL                                         $1      
          7        SEND_VAR                                                 $1
          8        DO_ICALL                                         $2      
          9        SEND_VAR                                                 $2
         10        DO_ICALL                                         $3      
         11      > RETURN                                                   $3
   33    12*     > RETURN                                                   null

End of function wp_array_unique3

Function wp_array_unique4:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AaU1X
function name:  wp_array_unique4
number of ops:  9
compiled vars:  !0 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV_INIT                                        !0      <array>
   38     1        INIT_FCALL                                               'array_keys'
          2        INIT_FCALL                                               'array_flip'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $1      
          5        SEND_VAR                                                 $1
          6        DO_ICALL                                         $2      
          7      > RETURN                                                   $2
   39     8*     > RETURN                                                   null

End of function wp_array_unique4

Function wp_array_unique5:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AaU1X
function name:  wp_array_unique5
number of ops:  9
compiled vars:  !0 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV_INIT                                        !0      <array>
   44     1        INIT_FCALL                                               'array_flip'
          2        INIT_FCALL                                               'array_flip'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $1      
          5        SEND_VAR                                                 $1
          6        DO_ICALL                                         $2      
          7      > RETURN                                                   $2
   45     8*     > RETURN                                                   null

End of function wp_array_unique5

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
184.47 ms | 1479 KiB | 23 Q