3v4l.org

run code in 300+ PHP versions simultaneously
<?php function __( $str ) { return $str; } function _doing_it_wrong( $func, $error_message, $version ) { trigger_error( $error_message, E_USER_NOTICE ); } class WP_List_Util { /** * The input array. * * @since 4.7.0 * @var array */ private $input = array(); /** * The output array. * * @since 4.7.0 * @var array */ private $output = array(); /** * Temporary arguments for sorting. * * @since 4.7.0 * @var string[] */ private $orderby = array(); /** * Constructor. * * Sets the input array. * * @since 4.7.0 * * @param array $input Array to perform operations on. */ public function __construct( $input ) { $this->output = $input; $this->input = $input; } public function pluck( $field, $index_key = null ) { $newlist = array(); if ( ! $index_key ) { /* * This is simple. Could at some point wrap array_column() * if we knew we had an array of arrays. */ foreach ( $this->output as $key => $value ) { if ( is_object( $value ) ) { if ( property_exists( $value, $field ) ) { $newlist[ $key ] = $value->$field; } } elseif ( is_array( $value ) ) { if ( array_key_exists( $field, $value ) ) { $newlist[ $key ] = $value[ $field ]; } } else { _doing_it_wrong( __METHOD__, __( 'Values for the input array must be either objects or arrays.' ), '6.2.0' ); } } $this->output = $newlist; return $this->output; } /* * When index_key is not set for a particular item, push the value * to the end of the stack. This is how array_column() behaves. */ foreach ( $this->output as $value ) { if ( is_object( $value ) ) { if ( property_exists( $value, $field ) ) { if ( property_exists( $value, $index_key ) ) { $newlist[ $value->$index_key ] = $value->$field; } else { $newlist[] = $value->$field; } } } elseif ( is_array( $value ) ) { if ( array_key_exists( $field, $value ) ) { if ( array_key_exists( $index_key, $value ) ) { $newlist[ $value[ $index_key ] ] = $value[ $field ]; } else { $newlist[] = $value[ $field ]; } } } else { // _doing_it_wrong( // __METHOD__, // __( 'Values for the input array must be either objects or arrays.' ), // '6.2.0' // ); } } $this->output = $newlist; return $this->output; } } function wp_list_pluck( $input_list, $field, $index_key = null ) { if ( ! is_array( $input_list ) ) { return array(); } $util = new WP_List_Util( $input_list ); return $util->pluck( $field, $index_key ); } /* * Test with an array of arrays. */ $input_list = array( array( '123' => '456' ), array( 'foo' => 'bar' ), array( 'foo' => 'baz' ), ); var_dump( array_column( $input_list, 'foo', null ) ); var_dump( wp_list_pluck( $input_list, 'foo', null ) ); /* * Test with an array of objects. */ $input_list = array( (object) array( '123' => '456' ), (object) array( 'foo' => 'bar' ), (object) array( 'foo' => 'baz' ), ); var_dump( wp_list_pluck( $input_list, 'foo', null ) );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EuHT5
function name:  (null)
number of ops:  33
compiled vars:  !0 = $input_list
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  131     0  E >   ASSIGN                                                   !0, <array>
  137     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'array_column'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 'foo'
          5        SEND_VAL                                                 null
          6        DO_ICALL                                         $2      
          7        SEND_VAR                                                 $2
          8        DO_ICALL                                                 
  138     9        INIT_FCALL                                               'var_dump'
         10        INIT_FCALL                                               'wp_list_pluck'
         11        SEND_VAR                                                 !0
         12        SEND_VAL                                                 'foo'
         13        SEND_VAL                                                 null
         14        DO_FCALL                                      0  $4      
         15        SEND_VAR                                                 $4
         16        DO_ICALL                                                 
  144    17        CAST                                          8  ~6      <array>
         18        INIT_ARRAY                                       ~7      ~6
  145    19        CAST                                          8  ~8      <array>
         20        ADD_ARRAY_ELEMENT                                ~7      ~8
  146    21        CAST                                          8  ~9      <array>
         22        ADD_ARRAY_ELEMENT                                ~7      ~9
  143    23        ASSIGN                                                   !0, ~7
  148    24        INIT_FCALL                                               'var_dump'
         25        INIT_FCALL                                               'wp_list_pluck'
         26        SEND_VAR                                                 !0
         27        SEND_VAL                                                 'foo'
         28        SEND_VAL                                                 null
         29        DO_FCALL                                      0  $11     
         30        SEND_VAR                                                 $11
         31        DO_ICALL                                                 
         32      > RETURN                                                   1

Function __:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EuHT5
function name:  __
number of ops:  3
compiled vars:  !0 = $str
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1      > RETURN                                                   !0
    5     2*     > RETURN                                                   null

End of function __

Function _doing_it_wrong:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EuHT5
function name:  _doing_it_wrong
number of ops:  8
compiled vars:  !0 = $func, !1 = $error_message, !2 = $version
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
    8     3        INIT_FCALL                                               'trigger_error'
          4        SEND_VAR                                                 !1
          5        SEND_VAL                                                 1024
          6        DO_ICALL                                                 
    9     7      > RETURN                                                   null

End of function _doing_it_wrong

Function wp_list_pluck:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EuHT5
function name:  wp_list_pluck
number of ops:  17
compiled vars:  !0 = $input_list, !1 = $field, !2 = $index_key, !3 = $util
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  117     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
  118     3        TYPE_CHECK                                  128  ~4      !0
          4        BOOL_NOT                                         ~5      ~4
          5      > JMPZ                                                     ~5, ->7
  119     6    > > RETURN                                                   <array>
  122     7    >   NEW                                              $6      'WP_List_Util'
          8        SEND_VAR_EX                                              !0
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !3, $6
  124    11        INIT_METHOD_CALL                                         !3, 'pluck'
         12        SEND_VAR_EX                                              !1
         13        SEND_VAR_EX                                              !2
         14        DO_FCALL                                      0  $9      
         15      > RETURN                                                   $9
  125    16*     > RETURN                                                   null

End of function wp_list_pluck

Class WP_List_Util:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EuHT5
function name:  __construct
number of ops:  6
compiled vars:  !0 = $input
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
   46     1        ASSIGN_OBJ                                               'output'
          2        OP_DATA                                                  !0
   47     3        ASSIGN_OBJ                                               'input'
          4        OP_DATA                                                  !0
   48     5      > RETURN                                                   null

End of function __construct

Function pluck:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 42
Branch analysis from position: 5
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 37
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 37
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 20
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 19
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 19
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 28
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 27
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
Branch analysis from position: 27
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
Branch analysis from position: 42
2 jumps found. (Code = 77) Position 1 = 44, Position 2 = 82
Branch analysis from position: 44
2 jumps found. (Code = 78) Position 1 = 45, Position 2 = 82
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 66
Branch analysis from position: 47
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 65
Branch analysis from position: 52
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 62
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 81
Branch analysis from position: 81
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 81
Branch analysis from position: 81
Branch analysis from position: 65
Branch analysis from position: 66
2 jumps found. (Code = 43) Position 1 = 68, Position 2 = 81
Branch analysis from position: 68
2 jumps found. (Code = 43) Position 1 = 70, Position 2 = 80
Branch analysis from position: 70
2 jumps found. (Code = 43) Position 1 = 72, Position 2 = 77
Branch analysis from position: 72
1 jumps found. (Code = 42) Position 1 = 80
Branch analysis from position: 80
1 jumps found. (Code = 42) Position 1 = 81
Branch analysis from position: 81
Branch analysis from position: 77
1 jumps found. (Code = 42) Position 1 = 81
Branch analysis from position: 81
Branch analysis from position: 80
Branch analysis from position: 81
Branch analysis from position: 82
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 82
filename:       /in/EuHT5
function name:  pluck
number of ops:  88
compiled vars:  !0 = $field, !1 = $index_key, !2 = $newlist, !3 = $value, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
   51     2        ASSIGN                                                   !2, <array>
   53     3        BOOL_NOT                                         ~6      !1
          4      > JMPZ                                                     ~6, ->42
   58     5    >   FETCH_OBJ_R                                      ~7      'output'
          6      > FE_RESET_R                                       $8      ~7, ->37
          7    > > FE_FETCH_R                                       ~9      $8, !3, ->37
          8    >   ASSIGN                                                   !4, ~9
   59     9        TYPE_CHECK                                  256          !3
         10      > JMPZ                                                     ~11, ->20
   60    11    >   INIT_FCALL                                               'property_exists'
         12        SEND_VAR                                                 !3
         13        SEND_VAR                                                 !0
         14        DO_ICALL                                         $12     
         15      > JMPZ                                                     $12, ->19
   61    16    >   FETCH_OBJ_R                                      ~14     !3, !0
         17        ASSIGN_DIM                                               !2, !4
         18        OP_DATA                                                  ~14
   59    19    > > JMP                                                      ->36
   63    20    >   TYPE_CHECK                                  128          !3
         21      > JMPZ                                                     ~15, ->28
   64    22    >   ARRAY_KEY_EXISTS                                         !0, !3
         23      > JMPZ                                                     ~16, ->27
   65    24    >   FETCH_DIM_R                                      ~18     !3, !0
         25        ASSIGN_DIM                                               !2, !4
         26        OP_DATA                                                  ~18
   63    27    > > JMP                                                      ->36
   68    28    >   INIT_FCALL                                               '_doing_it_wrong'
   69    29        SEND_VAL                                                 'WP_List_Util%3A%3Apluck'
   70    30        INIT_FCALL                                               '__'
         31        SEND_VAL                                                 'Values+for+the+input+array+must+be+either+objects+or+arrays.'
         32        DO_FCALL                                      0  $19     
         33        SEND_VAR                                                 $19
   71    34        SEND_VAL                                                 '6.2.0'
   68    35        DO_FCALL                                      0          
   58    36    > > JMP                                                      ->7
         37    >   FE_FREE                                                  $8
   76    38        ASSIGN_OBJ                                               'output'
         39        OP_DATA                                                  !2
   78    40        FETCH_OBJ_R                                      ~22     'output'
         41      > RETURN                                                   ~22
   85    42    >   FETCH_OBJ_R                                      ~23     'output'
         43      > FE_RESET_R                                       $24     ~23, ->82
         44    > > FE_FETCH_R                                               $24, !3, ->82
   86    45    >   TYPE_CHECK                                  256          !3
         46      > JMPZ                                                     ~25, ->66
   87    47    >   INIT_FCALL                                               'property_exists'
         48        SEND_VAR                                                 !3
         49        SEND_VAR                                                 !0
         50        DO_ICALL                                         $26     
         51      > JMPZ                                                     $26, ->65
   88    52    >   INIT_FCALL                                               'property_exists'
         53        SEND_VAR                                                 !3
         54        SEND_VAR                                                 !1
         55        DO_ICALL                                         $27     
         56      > JMPZ                                                     $27, ->62
   89    57    >   FETCH_OBJ_R                                      ~28     !3, !1
         58        FETCH_OBJ_R                                      ~30     !3, !0
         59        ASSIGN_DIM                                               !2, ~28
         60        OP_DATA                                                  ~30
   88    61      > JMP                                                      ->65
   91    62    >   FETCH_OBJ_R                                      ~32     !3, !0
         63        ASSIGN_DIM                                               !2
         64        OP_DATA                                                  ~32
   86    65    > > JMP                                                      ->81
   94    66    >   TYPE_CHECK                                  128          !3
         67      > JMPZ                                                     ~33, ->81
   95    68    >   ARRAY_KEY_EXISTS                                         !0, !3
         69      > JMPZ                                                     ~34, ->80
   96    70    >   ARRAY_KEY_EXISTS                                         !1, !3
         71      > JMPZ                                                     ~35, ->77
   97    72    >   FETCH_DIM_R                                      ~36     !3, !1
         73        FETCH_DIM_R                                      ~38     !3, !0
         74        ASSIGN_DIM                                               !2, ~36
         75        OP_DATA                                                  ~38
   96    76      > JMP                                                      ->80
   99    77    >   FETCH_DIM_R                                      ~40     !3, !0
         78        ASSIGN_DIM                                               !2
         79        OP_DATA                                                  ~40
   94    80    > > JMP                                                      ->81
   85    81    > > JMP                                                      ->44
         82    >   FE_FREE                                                  $24
  111    83        ASSIGN_OBJ                                               'output'
         84        OP_DATA                                                  !2
  113    85        FETCH_OBJ_R                                      ~42     'output'
         86      > RETURN                                                   ~42
  114    87*     > RETURN                                                   null

End of function pluck

End of class WP_List_Util.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.66 ms | 1482 KiB | 21 Q